
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

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@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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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 list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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
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@haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On 2015-04-28 12:15, Alexey Shmalko wrote:
I'm sorry, my example should've been:
[1,2,3] >>= \a -> [a+1] >>= \a -> return a
Or just: [1,2,3] >>= (return . (+1)) -- http://people.eisenbits.com/~stf/ http://www.eisenbits.com/ OpenPGP: 9EC2 5620 2355 B1DC 4A8F 8C79 0EC7 C214 E5AE 3B4E

On Sun, May 3, 2015 at 2:07 PM Stanislaw Findeisen < stf.list.haskell@eisenbits.com> wrote:
On 2015-04-28 12:15, Alexey Shmalko wrote:
I'm sorry, my example should've been:
[1,2,3] >>= \a -> [a+1] >>= \a -> return a
Or just:
[1,2,3] >>= (return . (+1))
Or just: [2,3,4] The point of the example was to desugar original do notation and show there are two `a`s involved.
participants (4)
-
Alexey Shmalko
-
Grzegorz Milka
-
Shishir Srivastava
-
Stanislaw Findeisen