简体   繁体   中英

What does prime mean in 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 (-) }

The prime ( ' ) is treated like any number in variable names, ie 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, eg

plus :: Int -> Int -> Int
plus = (+)

Prelude> 4 `plus` 5
9

A binary function f is usually applied to 2 arguments as fxy . 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM