In haskell I can see a lot of prime just like 'chainl1'
what does it mean?
expr = term `chainl1` addop
term = factor `chainl1` mulop
factor = parens expr <|> integer
mulop = do{ symbol "*"; return (*) }
<|> do{ symbol "/"; return (div) }
addop = do{ symbol "+"; return (+) }
<|> do{ symbol "-"; return (-) }
The prime ('
) is treated like any number in variable names, i.e. unless it's at the beginning you can use it just like a letter. Hence names such as foldl'
; generally those will refer some kind of "alternative" of a similar thing, in this case foldl
which is equivalent except for lazy evaluation.
However, there aren't actually any primes in your examples. Those are backticks. Surrounding a function with backticks lets you use it like an infix operator, e.g.
plus :: Int -> Int -> Int
plus = (+)
Prelude> 4 `plus` 5
9
A binary function f
is usually applied to 2 arguments as f x y
. However, there are certain binary functions (like elem
) for which it makes sense to see them infix and not postfix. To move a binary function to infix notation one encloses it in backticks (`
). Compare
intersect set1 set2 = [x | x <- set1, elem x set2]
with
intersect set1 set2 = [x | x<- set1, x `elem` set2]
The second one is closer to the mathematical notation.
See also the corresponding learn you a Haskell chapter
PS: You can do the reverse for operators. Usually an operator is infix (2 + 3
) but you can move it to prefix by enclosing it in parens ((+) 2 3
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With