他的意思是,数据类型 String 和 Int 是幺半群吗?
No, 两者都不String
nor Int
是幺半群。幺半群是一个三元组(S、⊕、e)其中 ⊕ 是二元运算符⊕ : S×S ⊕; S,这样对于所有元素a、b、c∈S它认为(a⊕b)⊕c=a⊕(b⊕c), and 电子与系统是一个“身份元素”,使得a⊕e=e⊕a=a. String
and Int
是类型,所以基本上是值的集合,但不是三元组。
文章说:
让我们以String
级联 and Int
添加作为
例子monoids有关系的。
所以作者显然还提到了二元运算符((++)
的情况下String
, and (+)
的情况下Int
)。身份(如果是空字符串String
and 0
的情况下Int
) 被隐式保留;在非正式英语对话中,将身份留给读者作为练习是很常见的。
Now given that we have two monoid structures (M, ⊕, em) and (N, ⊗, en), a function f : M → N (like length
) is then called a monoid homomorphism [wiki] https://en.wikipedia.org/wiki/Monoid#Monoid_homomorphisms given it holds that f(m1⊕m2)=f(m1)⊗f(m2) for all elements m1, m2∈M and that mapping also preserves the identity element: f(em)=en.
例如length :: String -> Int
是一个幺半群同态,因为我们可以考虑幺半群(String
, (++)
, ""
) and (Int
, (+)
, 0
)。它认为:
-
length (s1 ++ s2) == length s1 + length s2
(对全部String
s s1
and s2
); and
-
length "" == 0
.