Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Value access in lambda function (Divam)
   2. Re:  Value access in lambda function (Kim-Ee Yeoh)
   3. Re:  Value access in lambda function (Brandon Allbery)
   4. Re:  Value access in lambda function (Divam)
   5. Re:  Value access in lambda function (Brandon Allbery)


----------------------------------------------------------------------

Message: 1
Date: Sat, 29 Dec 2012 06:44:38 +0530
From: Divam <[email protected]>
Subject: [Haskell-beginners] Value access in lambda function
To: [email protected]
Message-ID:
        <CAKmh3ZsQDW0jaKmiQMPzDM0pBV=vycg9pvs3adtvo6qxnuh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121229/44814cd1/attachment-0001.htm>

------------------------------

Message: 2
Date: Sat, 29 Dec 2012 08:45:46 +0700
From: Kim-Ee Yeoh <[email protected]>
Subject: Re: [Haskell-beginners] Value access in lambda function
To: Divam <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
        <capy+zdtrixuxyamundyuvwjx67aatcb8qtmzyrdk+0do4mp...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

> 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 <[email protected]> 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
> [email protected]
> 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-0001.htm>

------------------------------

Message: 3
Date: Fri, 28 Dec 2012 20:59:03 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Value access in lambda function
To: Divam <[email protected]>
Cc: [email protected]
Message-ID:
        <cakfcl4wmupv6t1wrxtpmk5dunxeb63c-_x5bootthyyqi7q...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Fri, Dec 28, 2012 at 8:14 PM, Divam <[email protected]> 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
[email protected]                                  [email protected]
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-0001.htm>

------------------------------

Message: 4
Date: Sat, 29 Dec 2012 09:27:36 +0530
From: Divam <[email protected]>
Subject: Re: [Haskell-beginners] Value access in lambda function
To: Brandon Allbery <[email protected]>, [email protected]
Cc: [email protected]
Message-ID:
        <cakmh3zspkxpvhr0aa9ty_r0nwxme7hrz+wdmy0spsydkbst...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <[email protected]> wrote:

> On Fri, Dec 28, 2012 at 8:14 PM, Divam <[email protected]> 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
> [email protected]
> [email protected]
> 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-0001.htm>

------------------------------

Message: 5
Date: Sat, 29 Dec 2012 00:55:54 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Value access in lambda function
To: Divam <[email protected]>
Cc: [email protected]
Message-ID:
        <cakfcl4wn8ddzoyhyjaykbd32mzqrsfzgmcoitjhed-kuuwd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Fri, Dec 28, 2012 at 10:57 PM, Divam <[email protected]> wrote:

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

A function of multiple arguments is actually a function of a single
argument which returns a function that takes the next argument, and so on.
 Partial application therefore happens automatically.


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

You may be using a different definition of "pure" than Haskell does; the
point of pure functions in Haskell is that they cannot *change* anything
outside of the function scope, not that they can't *see* anything.


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

There are no variables involved.  "Variables" can vary; bindings are
immutable.  This is key to purity.

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121229/133878a6/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 54, Issue 47
*****************************************

Reply via email to