I came across this function
iter p f x = if (p x) then x else (iter p f (f x))
and I thought I'd give a go at defining the polymorphic types myself to understand the concept.
My thought was the following:
The function takes 3 parameters, so we have t1 -> t2 -> t3 -> T
pis being used inside the if condition so it must return abool, thereforet1 = a -> Boolfis also the same type aspbecause it is passed as an argument in the else block, thereforet2 = a -> Boolx is being used inside the if condition so it must return a bool, therefore
t1 = a -> Bool
But when i checked the type in the ghci, the type they gave me was
iter :: (t -> Bool) -> (t -> t) -> t -> t
Can someone please explain the reasoning behind this.
Thanks
This is correct as a starting point.
Correct.
Incorrect.
fis never used in the same way asp. In the else blockfis being applied toxand the result passed as the last argument toiter. From that we knowf xmust be the same type asxsof :: a -> a.Incorrect. In the if condition
xis being used only as an argument top. You established abovep :: a -> Bool. Thereforex :: a.Correct. You could also write this replacing
twithato be consistent in the notation - we usedaabove: