常见的Haskell运算符是否有明显的名称?

时间:2011-10-12 21:35:27

标签: haskell operators

我正在阅读Learn You a Haskell for Great Good,我从来不知道如何发音Haskell运算符。他们有“真实”的名字吗? ?

例如,你如何大声朗读这样的表达?

Just (+3) <*> Just 9

我知道>>=是“绑定”,但其他人呢?由于谷歌不考虑非字母数字字符,因此很难进行有效的搜索......

我意识到你可以创建自己的运算符,所以当然并非所有运算符都可以有名称,但我希望常见的运算符(例如ApplicativeMonad中定义的运算符)必须具有名称。 ..

5 个答案:

答案 0 :(得分:184)

以下是我发音的方式:

>>=     bind
>>      then
*>      then
->      to                a -> b: a to b
<-      bind              (as it desugars to >>=)
<$>     (f)map
<$      map-replace by    0 <$ f: "f map-replace by 0"
<*>     ap(ply)           (as it is the same as Control.Monad.ap)
$                         (none, just as " " [whitespace])
.       pipe to           a . b: "b pipe-to a"
!!      index
!       index / strict    a ! b: "a index b", foo !x: foo strict x
<|>     or / alternative  expr <|> term: "expr or term"
++      concat / plus / append
[]      empty list
:       cons
::      of type / as      f x :: Int: f x of type Int
\       lambda
@       as                go ll@(l:ls): go ll as l cons ls
~       lazy              go ~(a,b): go lazy pair a, b

答案 1 :(得分:38)

| sym  | pronunciation                                    |
|------|--------------------------------------------------|
| |    | "such that"                                      |
| <-   | "is drawn from"                                  |
| =    | "is defined to be" / "is defined as"             |
| ::   | "has type" / "of type" / "is of type"            |
| ->   | "a function that takes ... and returns a ..." /  |
|      |                          "function that maps" /  |
|      |                          "is a function from" /  |
|      |                                          "to"    |
| $    | "apply"                                          |
| _    | "whatever"                                       |
| !!   | "index"                                          |
| ++   | "concat"                                         |
| []   | "empty list"                                     |
| :    | "cons"                                           |
| \    | "lambda"                                         |
| =>   | "implies" / "then"                               |
| *>   | "then"                                           |
| <$>  | "fmap" / "dollar cyclops"                        |
| <$   | "map-replace by"                                 |
| <*>  | "ap" / "star cyclops"                            |
| .    | "pipe to" / "compose" / "dot"                    |
| <|>  | "or"                                             |
| @    | "as"                                             |
| ~    | "lazy"                                           |
| <=<  | "left fish"                                      |

答案 2 :(得分:27)

我个人最喜欢的是“左鱼”(<=<)和“右鱼”(>=>)。这些只是左派和右派Kleisli组成的monad运营商。撰写腥,撰写!

答案 3 :(得分:8)

我冒昧地将答案组合成一个非常简单的haskell程序,只有通过模式匹配才能尝试将haskell代码翻译成英文。我将其称为letterator因为它将符号转换为字母

-- letterator

main = translateLn <$> getLine >>= putStrLn

translateLn :: String -> String
translateLn = unwords . map t . words

t :: String -> String -- t(ranslate)

-- historical accurate naming
t "=" = "is equal too" -- The Whetstone of Witte - Robert Recorde (1557)

-- proposed namings
-- src http://stackoverflow.com/a/7747115/1091457
t ">>=" = "bind"
t "*>"  = "then"
t "->"  = "to"                   -- a -> b: a to b
t "<$"  = "map-replace by"       --  0 <$ f: "f map-replace by 0"
t "<*>" = "ap(ply)"              --  (as it is the same as Control.Monad.ap)
t "!!"  = "index"
t "!"   = "index/strict"         --  a ! b: "a index b", foo !x: foo strict x
t "<|>" = "or/alternative"       -- expr <|> term: "expr or term"
t "[]"  = "empty list"
t ":"   = "cons"
t "\\"  = "lambda"
t "@"   = "as"                   -- go ll@(l:ls): go ll as l cons ls
t "~"   = "lazy"                 -- go ~(a,b): go lazy pair a, b
-- t ">>"  = "then"
-- t "<-"  = "bind"              -- (as it desugars to >>=)
-- t "<$>" = "(f)map"
-- t "$"   = ""                  -- (none, just as " " [whitespace])
-- t "."   = "pipe to"           -- a . b: "b pipe-to a"
-- t "++"  = "concat/plus/append" 
-- t "::"  = "ofType/as"         -- f x :: Int: f x of type Int

-- additional names
-- src http://stackoverflow.com/a/16801782/1091457
t "|"   = "such that"
t "<-"  = "is drawn from"
t "::"  = "is of type" 
t "_"   = "whatever"
t "++"  = "append"
t "=>"  = "implies"
t "."   = "compose"
t "<=<" = "left fish"
-- t "="   = "is defined as"
-- t "<$>" = "(f)map"

-- src http://stackoverflow.com/a/7747149/1091457
t "$"   = "of" 

-- src http://stackoverflow.com/questions/28471898/colloquial-terms-for-haskell-operators-e-g?noredirect=1&lq=1#comment45268311_28471898
t ">>"  = "sequence"
-- t "<$>" = "infix fmap"
-- t ">>=" = "bind"

--------------
-- Examples --
--------------

-- "(:) <$> Just 3 <*> Just [4]" 
-- meaning "Cons applied to just three applied to just list with one element four"
t "(:)"  = "Cons"
t "Just" = "just"
t "<$>"  = "applied to"
t "3"    = "three" -- this is might go a bit too far
t "[4]"  = "list with one element four" -- this one too, let's just see where this gets us

-- additional expressions to translate from
-- src http://stackoverflow.com/a/21322952/1091457
-- delete (0, 0) $ (,) <$> [-1..1] <*> [-1..1]
-- (,) <$> [-1..1] <*> [-1..1] & delete (0, 0)
-- liftA2 (,) [-1..1] [-1..1] & delete (0, 0)
t "(,)" = "tuple constructor"
t "&" = "then" -- flipped `$`

-- everything not matched until this point stays at it is
t x = x

答案 4 :(得分:4)

+      plus
-      minus (OR negative OR negate for unary use)
*      multiply OR times
/      divide
.      dot OR compose
$      apply OR of