Small slip with $
mfricke1947 opened this issue · 4 comments
[I know very little Haskell, so please make allowances here.]
David you write:
"The important take-away message here, again, is that the following two lines are equivalent: both of them calculate f x, and then make the result available inside the (...) under the name y.
y = f x
(...)
f x $ \y -> (...)"
The issue is with the last line. $ expects a function on the left and a 'value' on the right. So shouldn't this be
\y -> (...) $ f x
or flip the arguments:
aFun x = x+x
y = aFun 2
aFun y -- 8
(£) = flip ($)
aFun 2 £ (\y -> (aFun y)) -- 8
Hope this helps. All the best, Martin
Hey there!
It’s true that ($) expects a function on the left, and a value to plug it into. In the example I’m giving, f x
is a function – f :: a -> (b -> c) -> c
, so f x :: (b -> c) -> c
. So we can write f x $ \y -> (...)
and it’s well-typed for suitably typed (...)
.
Here’s the term, with all subterms annotated with their types:
f x $ \y -> (...)
-- ^:: a -> (b -> c) -> c
f x $ \y -> (...)
-- ^:: a
f x $ \y -> (...)
-- ^^^:: (b -> c) -> c
f x $ \y -> (...)
-- ^:: b
f x $ \y -> (...)
-- ^^^^^:: c
f x $ \y -> (...)
-- ^^^^^^^^^^^:: b -> c
f x $ \y -> (...)
-- ^^^^^^^^^^^^^^^^^:: c
The same term could have been written using parentheses instead of ($)
, and is maybe a little clearer here?
f x (\y -> (...))
You’re welcome! As a final remark, there are several functions in the standard modules that are in continuation-passing style, such as bracket
.
Can we close this issue then? :-)