[Haskell-beginners] Value access in lambda function

Divam dfordivam at gmail.com
Sat Dec 29 04:57:36 CET 2012


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

Kim, This is a very useful insight



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

Brandon, I need to study this again, still dont understood how its related
to partial function application.



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, This is quite strange thing, the functions can access the globally
defined variables and therefore there output is dependent not just on
inputs but on those variables too. This goes against the understanding of
pure functions.
Also there may be  another way to look at this - all the global variables
are inputs to all the functions. I think this is how the haskell looks at
this scenario.


Thanks for your quick replies
Divam

On 29 December 2012 07:29, Brandon Allbery <allbery.b at gmail.com> wrote:

> 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/20121229/f9ac8f31/attachment.htm>


More information about the Beginners mailing list