Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell operator vs function precedence

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

list = map foo $ xs 

can be rewritten as

list = (map foo) $ (xs) 

and will eventually be

list = map foo xs 

My question used to be, why the first formulation would not be rewritten as

list = (map foo $) xs 

since function precedence is always higher than operator precedence, but I think that I have found the answer: operators are simply not allowed to be arguments of functions (except of course, if you surround them with parentheses). Is this right? If so, I find it odd, that there is no mention of this mechanic/rule in RWH or Learn you a Haskell, or any of the other places that I have searched. So if you know a place, where the rule is stated, please link to it.

-- edit: Thank you for your quick answers. I think my confusion came from thinking that an operator literal would somehow evaluate to something, that could get consumed by a function as an argument. It helped me to remember, that an infix operator can be mechanically translated to a prefix functions. Doing this to the first formulation yields

($) (map foo) (xs) 

where there is no doubt that ($) is the consuming function, and since the two formulations are equivalent, then the $ literal in the first formulation cannot be consumed by map.

like image 459
Boris Avatar asked Jun 26 '10 20:06

Boris


People also ask

Which has more precedence && or ||?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

Is Haskell left or right associative?

The makers of Haskell wanted function composition to be logically as similar as the $ operation. One of the makers of Haskell was a Japanese who found it more intuitive to make function composition right associative instead of left associative.

How does operator work Haskell?

In Haskell we have or operator to compare the values of the variable, this operator also comes under the lexical notation of the Haskell programming language. This operator works in the same way as any other programming language, it just returns true or false based on the input we have provided.

What is the dollar sign in Haskell?

In Haskell, $ is used as a function application operator. In an AutoHotkey script, a hotkey declared with $ is not triggered by a 'Send' command elsewhere in the script.


1 Answers

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.

like image 189
Don Stewart Avatar answered Oct 12 '22 10:10

Don Stewart