I've started learning Haskell and got problems. I try to understand lambda and created function mo06
mo06 f x = if f x
then x
else x * x
I can call that in ghci:
λ mo06 (== 1) 1
1
λ mo06 (== 1) 3
9
That works ok - no problems and no questions, but...
...I want to write my function as
mo08 f = fac (\x -> if f x then x else x * x)
where
fac
means factorial:
fac :: Num a => Int -> Int
fac n
| n == 0 = 1
| n > 0 = n * fac (n-1)
| otherwise = error "fac only defined on natural numbers"
and call it as previously
mo08 (==1) 3
I try and got an error:
Could't match expected type 'a -> a' with actual type 'Int'.
Changing fac
to (fromIntegr fac)
did not help.
Thank you.
Num a
constraint on yourInt -> Int
function is meaningless; just remove it. alternatively, change fromInt -> Int
toa -> a
. – Erik Kaplun