[Haskell-beginners] Variable re-use

Alexey Shmalko rasen.dubi at gmail.com
Tue Apr 28 10:15:48 UTC 2015


I'm sorry, my example should've been:

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

On Tue, Apr 28, 2015 at 1:12 PM Grzegorz Milka <grzegorzmilka at gmail.com>
wrote:

>  Hi,
>
> The variable is not reassigned, but hidden. The do notation is a syntactic
> sugar for:
>
> [1, 2, 3] >>= (\a ->
> [a + 1] >>= (\a ->
> return a))
>
>
> The second 'a' is inside a nested lambda function and therefore inside a
> nested scope. The first 'a' still exists, but the value to which it refers
> is hidden inside the second lambda function, where 'a' is bound to a
> different value.
>
> On 28.04.2015 11:53, Shishir Srivastava 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 listBeginners at haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>  _______________________________________________
> 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/3fed3435/attachment.html>


More information about the Beginners mailing list