[Haskell-cafe] let vs. where

C.M.Brown cmb21 at kent.ac.uk
Wed Nov 14 04:16:55 EST 2007

Hi David,

> A let clause would work fine here:
> someFunction ls a b c = let listLen = length ls
>                             someAuxFunction x y = ... listLen ...
>                             someOtherFunction x y = ... listLen ...
>                         in
>                         ... listLen ...
> it's just that you don't want to mix let and where clauses, because then
> things get confusing.  Even if it worked with both, noone would know the
> binding rules.

Possibly in that case, but there are cases where I believe they are not
the same.

For example:

gg n = ([1..,10^6*n], [1..10^6*n])

exp = (fst $ gg 1000, snd $ gg 1000)

this could be captured nicely in a where clause:

exp = (fst blah, snd blah) where blah = gg 1000

But a let would have to be placed in both elements of the tuple - and
therefore being evaluated twice (unless the implementation is smart enough
to work out they can be shared?):

exp = (let blah = g 1000 in fst blah, let blah = g 1000 in snd blah)

Kind regards,

More information about the Haskell-Cafe mailing list