Consider the following lambda function in Haskell:
(\x g n -> g (x * n))
It takes two parameters: a Num named x and a function g which takes a Num named n and returns something else. The lambda function returns another function of the same type as g:
(\x g n -> g (x * n)) :: Num a => a -> (a -> t) -> a -> t
What I don't understand is what does the expression g (x * n) actually represent. For example consider the following use case:
((\x g n -> g (x * n)) 2 id)
In this case x is 2 and g is id. However what is n? What does g (x * n) represent? By simple substitution it can be reduced to id (2 * n). Is this the same as id . (2 *)? If so then why not simply write (\x g -> g . (x *))?
I'm going to contradict chirlu. (\x g n -> g (x * n)) is a function of one argument.
Because all functions only take one argument. It's just that that function returns another function, which returns another function.
Desugared, it's the same as
\x -> \g -> \n -> g (x * n)
Which is pretty close to its type
Num a => a -> (a -> b) -> a -> b
Expanding your use case:
(\x g n -> g (x * n)) 2 id
Let's expand that
(\x -> \g -> \n -> g (x * n)) 2 id
Which is the same as
((\x -> \g -> \n -> g (x * n)) 2) id
Now we can apply the inner function to its argument to get
(let x = 2 in \g -> \n -> g (x * n)) id
or
(\g -> \n -> g (2 * n)) id
Now we can apply this function to its argument to get
let g = id in \n -> g (2 * n)
or
\n -> id (2 * n)
Which, via inspection, we can state is equivalent to
\n -> 2 * n
Or, point-free
(2*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With