2.5 local definitions let expressions and where clauses

So far we've really only dealt with functions, so let's look at more variables, specifically variables that live within the scope of a function. Haskell's methodology for this is via let:

f1 n = let phi    = (1 + root5) / 2
           phibar = -1/phi
           root5  = sqrt 5
       in (phi^n - phibar^n) / root5

Note that let can be used to define functions and other objects. However, the opposite of this is via where:

f1 n = (phi^n - phibar^n) / root5
       where phi    = (1 + root5)/2
             phibar = -1/phi
             root5  = sqrt 5

Haskell is a language written by mathematicians, thanks Christopher Strachey.

Note that the ordering seems to be pretty minimal, i.e. root5 is defined at the end. This is... ok, but to make it more readable:

f1 n = (phi^n - phibar^n) / root5
       where phi = (1 + root5)/2
                where root5 = sqrt 5
             phibar = -1/phi

This is fine because where is appended after an equality, and setting variables counts as that.