I am trying to learn haskell and could not configure it out, why following code snippet can not get compiled:
*Uncurry> applyTwice f x = f f x
<interactive>:14:20: error:
• Occurs check: cannot construct the infinite type:
t ~ t -> t2 -> t1
• In the first argument of ‘f’, namely ‘f’
In the expression: f f x
In an equation for ‘applyTwice’: applyTwice f x = f f x
• Relevant bindings include
x :: t2 (bound at <interactive>:14:14)
f :: t -> t2 -> t1 (bound at <interactive>:14:12)
applyTwice :: (t -> t2 -> t1) -> t2 -> t1
(bound at <interactive>:14:1)
This would be fine:
applyTwice f x = f (f x)
In haskell function application is left associative, the first code snippet would be apply like:
(f f) x
why (f f) x
it is wrong?
f x y
for((f x) y)
. It is only a notational convention. It does not mean that application is associative, i.e. that((f x) y)
must be equal to(f (x y))
. $\endgroup$ – chi Mar 23 '17 at 18:19