Haskell/Solutions/Variables and functions

← Back to Variables and functions


Exercises
  • Explain how GHCi evaluates quadruple 5.
  • Define a function that subtracts 12 from half its argument.
quadruple x = double (double x)
quadruple 5 = double (double 5)
quadruple 5 = double 10
quadruple 5 = 20


let subtractHalf x = (half x) - 12


Exercises
  • Write a function to calculate the volume of a box.
  • Approximately how many stones are the famous pyramids at Giza made up of? Use GHCi for your calculations.
let volumeBox w h d = w * h * d


let stones l | l==1 = 1 | otherwise = l*l + stones (l-1)


Exercises
  • Write a function to calculate the volume of a cylinder.
let volumeCylinder r h = h * area r
Last modified on 23 December 2010, at 08:43