I've a beginner's question about types in Haskell: Having a function like:
f i xs = (sort xs) !! i
How do I define the function f0 xs = f 0 xs without explicit use of xs?
Just taking
f0 = f 0
does not work...
ghci shows me the folling types:
f :: Ord a => Int -> [a] -> a
f0 :: [()] -> ()
But ":t f 0" gives f 0 :: Ord a => [a] -> a.
Why is that? Why do I get this type for f0? Why is there any difference between the type of "f0" and the type of "f 0"?
Thanks a lot in advance for any suggestions
It has nothing to do with your particular definitions: if you do it (as you should!) with the standard implementation, the same thing happens.
Anyway, first you should give
fa signature.If you do that for
f0as well, all will work fine:Now the question is, why would ghci deduce such a stupid signature? It's the Dreaded Monomorphism Restriction's fault. That means, whenever you define something "as a constant(-applicative form)", i.e. a simple equation
then the compiler refuses to give that automatically a polymorphic signature (like, with
atype variables in it). Instead, it defaults to the "simplest usable" type, which for anOrdconstraint unfortunately is the completely useless().You can turn the monomorphism restriction off, then it'll work even without a signature:
But honestly, on the top level you should always use manual signatures anyway.