Haskell: type inference and function composition

后端 未结 2 1246
我寻月下人不归
我寻月下人不归 2020-12-31 06:12

This question was inspired by this answer to another question, indicating that you can remove every occurrence of an element from a list using a function defined as:

相关标签:
2条回答
  • 2020-12-31 06:43

    It is also worth noting that if you don't assign a name to the expression, typechecker seems to avoid type defaulting:

    Prelude> :t filter . (/=)
    filter . (/=) :: (Eq a) => a -> [a] -> [a]
    
    0 讨论(0)
  • 2020-12-31 07:03

    Why is Haskell inferring such a specific type for the function?

    GHCi is using type defaulting, to infer a more specific type from a set of possibles. You can avoid this easily by disabling the monomorphism restriction,

    Prelude> :set -XNoMonomorphismRestriction
    Prelude> let removeall = filter . (/=)
    Prelude> :t removeall 
    removeall :: (Eq a) => a -> [a] -> [a]
    
    0 讨论(0)
提交回复
热议问题