Jump to content
Wikibooks The Free Textbook Project

Haskell/Solutions/Variables and functions

From Wikibooks, open books for an open world

← Back to Variables and functions

Exercises
  • Explain how GHCi evaluates quadruple 5.
  • Define a function that subtracts 12 from half its argument.
quadruplex=double(doublex)
quadruple5=double(double5)
quadruple5=double10
quadruple5=20


subtractHalfx=x/2-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.
volumeBoxwhd=w*h*d


One approach for the Giza problem is starting from the pyramid volume and the estimated stone volume. Using general functions for the task, that might look like this in GHCi:

Prelude> let volumeBox w h d = w * h * d -- Just as above.
Prelude> let volumeSquarePyramid b h = b * b * h / 3
Prelude> volumeSquarePyramid 230.4 146.5 / volumeBox 1 1 1
2592276.48


Exercises
  • Write a function to calculate the volume of a cylinder.
volumeCylinderrh=h*arear

AltStyle によって変換されたページ (->オリジナル) /