Haskell/Solutions/Variables and functions
From Wikibooks, the open-content textbooks collection
← Back to Variables and functions
| Exercises |
|---|
|
Say I type something in like this (don't type it in yet): Prelude> let r = 0 Prelude> let area r = pi * r ^ 2 Prelude> area 5
|
1 and 2) The r in let r = 0 is global over the whole GHCi session, but the r in the function definition overrides it, because it lives in a deeper level of scope. area 5 returns 78.53981633974483, like it should do.
| Exercises |
|---|
| Write a function to calculate the volume of a box. A box has width, height and depth. You have to multiply them all to get the volume. |
let volumeBox w h d = w * h * d
| Exercises |
|---|
| Write a function to calculate the volume of a cylinder. The volume of a cylinder is the area of the base, which is a circle (you already programmed this function in this chapter, reuse it) multiplied by the height. |
let volumeCylinder r h = h * area r