[Haskell-beginners] Value access in lambda function

Brandon Allbery allbery.b at gmail.com
Sat Dec 29 02:59:03 CET 2012


On Fri, Dec 28, 2012 at 8:14 PM, Divam <dfordivam at gmail.com> wrote:

> 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).
>

They can access any bindings in the same scope; since a lambda binding does
not define a new scope in the same way a top level binding does, the
earlier bindings are still in scope and accessible.

Consider this:  a multi-parameter binding such as

    \a b c -> a + b + c

is really the same thing as

    \a -> (\b -> (\c -> a + b + c))

(This is, in fact, how partial function application works.)  a is still in
scope when b is defined, a and b are still in scope when c is defined.

What you can't do in a pure function is *change* them; all bindings are
read only, whether in or inward of their scope.  Inward scopes can redefine
bindings, but those new bindings are unrelated to the outer ones.

Also consider this:  in Haskell, a function is a binding like any other.
 If you couldn't refer to bindings from outer scopes, you couldn't define
new functions!  Well, aside from anonymous ones/lambdas, which are useful
but somewhat limiting if you can't name and reuse them.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121228/0e5f1279/attachment.htm>


More information about the Beginners mailing list