[Haskell-cafe] let vs. where
Justin Bailey
jgbailey at gmail.com
Tue Nov 13 14:41:20 EST 2007
On Nov 13, 2007 10:56 AM, John Lato <jwlato at gmail.com> wrote:
> I know there are several important differences between let-expressions
> and where-clauses regarding scoping and the restriction of "where" to
> a top-level definition. However, frequently I write code in which
One place I find it useful is when there is a common computed value
that is used throughout a function definition. For example, imagine
some function that uses the length of a list passed in:
someFunction ls a b c = ... (length ls)
where
someAuxFunction x y = ... length ls ..
someOtherFunction x y = ... length ls ...
a where clause can capture that calculation, make sure it's only done
once, and shared throughout the function definition:
someFunction ls a b c = ... listLen ...
where
listLen = length ls
someAuxFunction x y = ... listLen ...
someOtherFunction x y = ... listLen ...
Notice a let clause wouldn't do it above, because "length ls" is
called inside other functions defined in the where clause. Of course
everything could be moved to a "let" clause in the function body. At
that point I think it's purely stylistic.
Justin
More information about the Haskell-Cafe
mailing list