这是 Haskell 中的幺半群示例:
> import Data.Monoid
> Sum 5 <> Sum 6 <> Sum 10
Sum {getSum = 21}
> mconcat [Sum 5, Sum 6, Sum 10]
Sum {getSum = 21}
> getSum $ mconcat $ map Sum [5, 6, 10]
21
> getProduct $ mconcat $ map Product [5, 6, 10]
300
这是 Clojure 中的幺半群示例:
(defn plus-monoid
([]
0)
([a b]
(+ a b)))
(plus-monoid)
(plus-monoid 3 4)
(reduce plus-monoid [2 3 4])
这是 Haskell 中的环示例:
module Rings where
newtype Matrix r = M [[r]] deriving (Eq,Show)
instance Num r => Num (Matrix r) where
M [[a,b],[c,d]] + M [[a',b'],[c',d']] = M [[a+a',b+b'],[c+c',d+d']]
negate (M [[a,b],[c,d]]) = M [[-a,-b],[-c,-d]]
M [[a,b],[c,d]] * M [[e,f],[g,h]] = M [[a*e+b*g, a*f+b*h] ,[c*e+d*g, c*f+d*h]]
fromInteger n = M [[fromInteger n, 0],[0, fromInteger n]]
> M [[1,2],[3,4]] - M [[1,0],[0,1]]
M [[0,2],[3,3]]
> M [[2,0],[0,3]] * M [[1,2],[3,4]]
M [[2,4],[9,12]]
这是基于此的 Clojure 中的环示例:
(defprotocol ring
(plus [x y])
(mult [x y])
(neg [x])
(zero [])
(one []) )
似乎 - (借用 Java 用语)环和幺半群之间的区别在于环具有“要实现的接口上的附加方法”。(也许我错了)。现在对我来说,这会对关联性产生影响——但我还没有完全理解这一点。
我的问题是:幺半群和环之间的差异意味着什么?