Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  OOP exercise with closures (Lawrence Bottorff)
   2. Re:  OOP exercise with closures (Francesco Ariis)
   3. Re:  OOP exercise with closures (Lawrence Bottorff)


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

Message: 1
Date: Tue, 29 Dec 2020 15:43:46 -0600
From: Lawrence Bottorff <borg...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] OOP exercise with closures
Message-ID:
        <cafahfsuwry06e3oobh9usqbpf8mkgn2xyqpyykthnud6y84...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

First, thanks for all the help you've offered. Often enough, it's so much
that it takes me time to sift through all the good stuff. Thanks again.

Okay, I'm in Lesson 10 of *Get Programming with Haskell * and we're
creating an OOP-like world with closures. The first step is a cup object
with one attribute, its ounce size. Here's a "constructor"

cup :: t1 -> (t1 -> t2) -> t2
cup flOz = \message -> message flOz

so this returns upon use

> myCup = cup 6

myCup which has "internally" a lambda function

(\message -> message) 6

waiting, correct?

Now a "method"

getOz aCup = aCup (\foz -> foz)

creates a closure on the lambda function (\foz -> foz) . So upon calling

> getOz myCup
6

I'm guessing myCup (\foz -> foz) was evaluated, but I don't understand how
the 6 that went in as the bound variable in the constructor came out again
with getOz. As I understand it, the cup constructor creates a closure
around the given bound argument flOz -- which is confusing because I
thought closures "carried" free variables, not bound variables. Perhaps the
getOz lambda function (\foz -> foz) replaces completely the (\message ->
message) lambda function? So the constructor could have been written

cup flOz =  (\message -> message) flOz

In any case I'm shaky on how A) cup 6 sets up/stores the 6 in the creation
of myCup and then how getOz pops it out again.

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20201229/c74f98a3/attachment-0001.html>

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

Message: 2
Date: Tue, 29 Dec 2020 23:06:54 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] OOP exercise with closures
Message-ID: <20201229220654.GA10168@extensa>
Content-Type: text/plain; charset=utf-8

Il 29 dicembre 2020 alle 15:43 Lawrence Bottorff ha scritto:
> Okay, I'm in Lesson 10 of *Get Programming with Haskell * and we're
> creating an OOP-like world with closures. The first step is a cup object
> with one attribute, its ounce size. Here's a "constructor"
> 
> cup :: t1 -> (t1 -> t2) -> t2
> cup flOz = \message -> message flOz
> 
> so this returns upon use
> 
> > myCup = cup 6
> 
> myCup which has "internally" a lambda function
> 
> (\message -> message) 6
> 
> waiting, correct?

Not exactly. `myCup` is a function with takes another function as input

    λ> :t myCup
    myCup :: (Integer -> t2) -> t2

and the body looks like this

    \f -> f 6

`\f -> f 6`  is different from  `(\f -> f) 6`!


> Now a "method"
> 
> getOz aCup = aCup (\foz -> foz)
> 
> creates a closure on the lambda function (\foz -> foz) . So upon calling
> 
> > getOz myCup
> 6
> 
> I'm guessing myCup (\foz -> foz) was evaluated, but I don't understand how
> the 6 that went in as the bound variable in the constructor came out again
> with getOz.

To recap, `myCup` expands to:

    myCup
    cup 6
    \message -> message 6

Now, applying `getOz` to it…

    getOz myCup
    myCup (\foz -> foz)
    (\message -> message 6) (\foz -> foz)
    (\foz -> foz) 6
    6



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

Message: 3
Date: Tue, 29 Dec 2020 21:12:29 -0600
From: Lawrence Bottorff <borg...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] OOP exercise with closures
Message-ID:
        <CAFAhFSVT6EHAbgUUf7COrVw6Pvh_CmEDioQ=MjD19=gn+ky...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thanks! Since I studied lambda calc a bit I understand the steps

(\message -> message 6) (\foz -> foz)
(\foz -> foz) 6
 6

But this is so bizarre! To have a "constructor" that creates an instance,
which is then really a holder of a lambda calculation is a mind-bender.

On Tue, Dec 29, 2020 at 4:07 PM Francesco Ariis <fa...@ariis.it> wrote:

> Il 29 dicembre 2020 alle 15:43 Lawrence Bottorff ha scritto:
> > Okay, I'm in Lesson 10 of *Get Programming with Haskell * and we're
> > creating an OOP-like world with closures. The first step is a cup object
> > with one attribute, its ounce size. Here's a "constructor"
> >
> > cup :: t1 -> (t1 -> t2) -> t2
> > cup flOz = \message -> message flOz
> >
> > so this returns upon use
> >
> > > myCup = cup 6
> >
> > myCup which has "internally" a lambda function
> >
> > (\message -> message) 6
> >
> > waiting, correct?
>
> Not exactly. `myCup` is a function with takes another function as input
>
>     λ> :t myCup
>     myCup :: (Integer -> t2) -> t2
>
> and the body looks like this
>
>     \f -> f 6
>
> `\f -> f 6`  is different from  `(\f -> f) 6`!
>
>
> > Now a "method"
> >
> > getOz aCup = aCup (\foz -> foz)
> >
> > creates a closure on the lambda function (\foz -> foz) . So upon calling
> >
> > > getOz myCup
> > 6
> >
> > I'm guessing myCup (\foz -> foz) was evaluated, but I don't understand
> how
> > the 6 that went in as the bound variable in the constructor came out
> again
> > with getOz.
>
> To recap, `myCup` expands to:
>
>     myCup
>     cup 6
>     \message -> message 6
>
> Now, applying `getOz` to it…
>
>     getOz myCup
>     myCup (\foz -> foz)
>     (\message -> message 6) (\foz -> foz)
>     (\foz -> foz) 6
>     6
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20201229/8c67bab3/attachment-0001.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 149, Issue 20
******************************************

Reply via email to