I am trying to understand how this factorial example works using the function fix :: (a -> a) -> a
.
factabs :: (Num a, Eq a) => (a -> a) -> a -> a
factabs fact 0 = 1
factabs fact x = x * fact (x-1)
f :: (Num a, Eq a) => a -> a
f = fix factabs
I do not see why fix factabs
has this type... fix
expects a function of type a -> a
, how can we apply it to a function of type (a -> a) -> a -> a
(a function that takes two parameters)? I am totally confused...
Basically I was trying to figure out how to convert the function limit
below to something that uses fix
. Would appreciate any help.
limit f n
| n == next = n
| otherwise = limit f next
where
next = f n