How to provide explicit type declarations for functions when using GHCi?
Solution 1:
Is there a way provide type declarations in GHCi?
let numUniques' :: (Eq a) => [a] -> Int; numUniques' = length . nub
Or is there another way to define functions like these which doesn't require type declarations?
If you turn off the monomorphism restriction with -XNoMonomorphismRestriction
, it will infer the right type.
Solution 2:
Note that you can also avoid the monomorphism restriction simply by adding "points" (i.e. explicit variables) back to your expression. So this also gives the correct type:
let numUniques x = length . nub $ x
Solution 3:
The GHC User's Guide shows two additional ways to achieve this. This subsection introduces the :{
... :}
construct, which can be used as follows:
> :{
| numUniques :: (Eq a) => [a] -> Int
| numUniques = length . nub
| :}
Alternatively, you can enable multiline mode:
> :set +m
> let
| numUniques :: (Eq a) => [a] -> Int
| numUniques = length . nub
|