[Haskell-beginners] Variable re-use

Alexey Shmalko rasen.dubi at gmail.com
Tue Apr 28 10:11:47 UTC 2015


If you rewrite your example without do notation, you'll get:

[1,2,3] >>= \a -> [a+1] >>= return a

You can notice that first `a` and the last one are different things. They
are just different bindings that happen to have the same name (You'll get
the shadowing warning if enable -Wall). They have nothing in common and can
have different types as well.

On Tue, Apr 28, 2015 at 12:53 PM Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> Hi,
>
> Please can anyone explain how does 'a' get re-used in the code below. My
> understanding so far of haskell is that variables are not allowed to mutate
> or re-assigned.
>
> ---
> do
> a <- [1,2,3]
> a <- [a+1]
> return a
>
> [2,3,4]
> ---
>
> Thanks,
> Shishir
>
>  _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150428/08b040ed/attachment.html>


More information about the Beginners mailing list