Haskell operator vs function precedence

后端 未结 5 434
难免孤独
难免孤独 2020-12-01 05:30

I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code

list = map foo $ xs
         


        
5条回答
  •  一生所求
    2020-12-01 06:00

    Firstly, application (whitespace) is the highest precedence "operator".

    Secondly, in Haskell, there's really no distinction between operators and functions, other than that operators are infix by default, while functions aren't. You can convert functions to infix with backticks

    2 `f` x
    

    and convert operators to prefix with parens:

    (+) 2 3
    

    So, your question is a bit confused.

    Now, specific functions and operators will have declared precedence, which you can find in GHCi with ":info":

    Prelude> :info ($)
    ($) :: (a -> b) -> a -> b   -- Defined in GHC.Base
    
    infixr 0 $
    
    Prelude> :info (+)
    
    class (Eq a, Show a) => Num a where
      (+) :: a -> a -> a
    
    infixl 6 +
    

    Showing both precedence and associativity.

提交回复
热议问题