I have the following Haskell function:
g :: (a -> a) -> a
g f = x
where x = f x
Could somebody please explain, what kind of animal is that, why the function is correct and how it works?
I've tried to pass some function to it, e.g.
g (\x -> x + x)
or g (\xs -> head xs)
, but GHC returns me not an instance of Num
or cannot construct an infinite type
, accordingly. Moreover, there is an infinite loop in the first case. I am confused even more about the x
, which is f x
, - because where do I get x
?
I would be very glad for a complete explanation.