I'm learning Haskell and I've been experimenting with partial application. I tried to pertially apply sortBy. I don't undestand the type of the resulting function. And how should it be done properly?
let mf = sortBy compare
:t mf
mf :: [()] -> [()]
I'm learning Haskell and I've been experimenting with partial application. I tried to pertially apply sortBy. I don't undestand the type of the resulting function. And how should it be done properly?
let mf = sortBy compare
:t mf
mf :: [()] -> [()]
This is because of the dreaded monomorphism restriction and ghci's defaulting behaviour. This should solve it:
λ> :set -XNoMonomorphismRestriction
λ> import Data.List (sortBy)
λ> let mf = sortBy compare
λ> :t mf
mf :: Ord a => [a] -> [a]
The short story being because of monomorphic restriction, the compiler will try to reduce your function definition to a single type (in your case [()] -> [()]
). But without the restriction, you get a polymorphic type (Ord a => [a] -> [a]
) as constrained by the typeclass.
You're seeing an artifact the monomorphism restriction.
Prelude Data.List> :set -XNoMonomorphismRestriction
Prelude Data.List> let mf = sortBy compare
Prelude Data.List> :t mf
mf :: Ord a => [a] -> [a]