Haskell/Solutions/Variables and functions

From Wikibooks, open books for an open world
< Haskell | Solutions
Jump to: navigation, search

← 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
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export