[Haskell-beginners] OOP exercise with closures
borgauf at gmail.com
Tue Dec 29 21:43:46 UTC 2020
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
Now a "method"
getOz aCup = aCup (\foz -> foz)
creates a closure on the lambda function (\foz -> foz) . So upon calling
> getOz myCup
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners