
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