[Haskell-beginners] Value access in lambda function

Kim-Ee Yeoh ky3 at atamo.com
Sat Dec 29 02:45:46 CET 2012


> I dont understand that how can the last labda function 'F2' has access to
value 'val1' since it was passed to the input of 'F1' and was not an input
to 'F2'

This is ONE nested function, not F1 >>? F2:

\(val1, s) -> | Just (s, (s + 1)) >>?              -- F1
\(val2, s) -> Just (val, (val1, (val2, (s)))) |   -- F2

The vertical bars delineate the body of the \(val1,s) abstraction. That's
why val1 is still in scope in the second line.


-- Kim-Ee


On Sat, Dec 29, 2012 at 8:14 AM, Divam <dfordivam at gmail.com> wrote:

> Following is a simple program which uses monadic type binder.
> I dont understand that how can the last labda function 'F2' has access to
> value 'val1' since it was passed to the input of 'F1' and was not an input
> to 'F2'
>
>
> (>>?) :: Maybe a -> (a -> Maybe b) -> Maybe b
> Nothing >>? _ = Nothing
> Just v >>? f = f v
>
> var_access_test val =
>             Just (val, (val + 1)) >>?
>             \(val1, s) -> Just (s, (s + 1)) >>?              -- F1
>             \(val2, s) -> Just (val, (val1, (val2, (s))))    -- F2
>
>
> -- I guess
> -- F2 :: (Int, Int) -> Maybe (Int, (int, (Int. Int)))
> -- So how can the F2 function return 'val1' even though it was not an
> input to it.
> -- output of var_access_test 3
> -- Just (3,(3,(4,5)))
>
>
> Although this might be something very trivial but I am stuck here and not
> able to understand how this is working. (considering the functions to be
> pure they can only access values passed to them as input).
>
> - Divam
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121229/6f3fa9d1/attachment.htm>


More information about the Beginners mailing list