Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does prime mean in haskell?

Tags:

haskell

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 (-) }
like image 838
user3239558 Avatar asked Apr 04 '14 21:04

user3239558


2 Answers

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

like image 50
leftaroundabout Avatar answered Oct 04 '22 01:10

leftaroundabout


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)

like image 34
Mihai Maruseac Avatar answered Oct 04 '22 01:10

Mihai Maruseac