[Haskell-cafe] Re: Do expression definition

Thomas Davie tom.davie at gmail.com
Mon Sep 13 05:38:12 EDT 2010

On 13 Sep 2010, at 10:28, Gleb Alexeyev wrote:

> On 09/13/2010 12:23 PM, Michael Lazarev wrote:
>> 2010/9/13 Henning Thielemann<lemming at henning-thielemann.de>:
>>> It means that variables bound by let, may be instantiated to different types
>>> later.
>> Can you give an example, please?
> testOk = let f = id in (f 42, f True)
> --testNotOk :: Monad m => m (Int, Bool)
> --testNotOk = do f <- return id
> --               return (f 42, f True)
> Try uncommenting the 'testNotOk' definition.

There's no "later" here at all.

Two seperate definitions in a Haskell program act as if they have always been defined, are defined, and always will be defined, they are not dealt with in sequence (except for pattern matching but that doesn't apply here).

Instead, what's going on here is scoping.  The f in testOk is a different f to the one in testNotOkay, distinguished by their scope.

Finally, this is not how you use a let in a do expression, here's how you should do it:

testOk2 :: Monad m => m (Int, Bool)
testOk2 = do let f = id
             return (f 42, f True)


Tom Davie

More information about the Haskell-Cafe mailing list