Skip to content

Latest commit

 

History

History
47 lines (36 loc) · 1.64 KB

06.md

File metadata and controls

47 lines (36 loc) · 1.64 KB

Scope

It is a common pattern in software engineering to split up complex expressions to small subexpressions and compose them. This way they become more comprehensible to the human mind. So far every expression we have defined can be used in every other expression. This is referred to as top level binding. However if the purpose of splitting up an expression is not code reuse it is beneficial to decrease the scope of visibility.

The let expression

Local bindings can be created between let and in. These bindings can be used only in the expression following in.

cylinderVolume :: Double -> Double -> Double
cylinderVolume radius height = let square x = x * x
                                   baseArea = (square radius) * pi
                                in baseArea * height

Let expressions can be used everywhere where an expression can be used.

Exercise

  • Split up the following function to subexpressions using let.
averageCircleSpeed :: Double -> [Double] -> Double
averageCircleSpeed radius laps = 2 * radius * pi / ((sum laps) / (fromIntegral (length laps)))

Where

Unlike let expressions, where is not an expression. It is a special construct that can be used only in specific places. One possible place is at the end of function definitions. Definitions following the where keyword can be used in the whole function body including let expressions.

cylinderVolume :: Double -> Double -> Double
cylinderVolume radius height = baseArea * height
  where square x = x * x
        baseArea = (square radius) * pi

Exercise

  • Split up averageCircleSpeed using where.