| paul Hudak in his 'gentle introduction to haskell" says that a where clause
| is allowed only at the top level of a set of equations or case expression.
|
| So you cannot declare
| let
| f x = z / y where z = x + y
| in ....
|
| I do not know the reason why this restriction has been put.
| I see the above definition perfectly alright.
| Can anybody send me the reason for the above restriction.
|
| -- [EMAIL PROTECTED]
The wording "top level" was perhaps unfortunate. Your example is,
in fact, perfectly all right. The point is that
z / y where z = x + y
is not an expression. Although
f x = z / y where z = x + y
is equivalent to
f x = let z = x + y in z / y
,
f x = (let z = x + y in z / y) + 1
is acceptable, whereas
f x = (z / y where z = x + y) + 1
is syntactically incorrect. Thus, one often has a choice of using
a "let" or a "where" for supporting definitions, but "let" cannot
scope over multiple guarded righthand sides or cases, and "where"
cannot be used in a nested expression.
On a slightly different subject, as a matter of taste and readability,
I recommend avoiding deeply nested definitions in general and using
"where" clauses except where a binding _must_ scope over a
subexpression (as arises, for example, in continuation-passing style).
For example, the last example above is probably better rendered as
f x = z / y + 1 where z = x + y
or perhaps
f x = w + 1 where w = z / y
z = x + y
(Others may disagree.)
--Joe