GDa*_* 69 1 string haskell integer monoids option-type
我\xc2\xb4ve陷入了一点困惑,也许Int和Maybe String是否都是Haskell中的幺半群,因为我真的不知道可以将哪些函数应用于具有中性元素和关联性规则的幺半群方面的Int或String!有人可以帮我吗?
\nMonoid任何事物Maybe a只要有一个实例,就有a一个Monoid实例;两个值的组合Just只是根据其实例组合包装的值,而与任何组合的值Nothing都是其本身Nothing。
StringMonoid由于是[Char];的别名而有一个实例 所有列表都是通过列表串联以空列表作为标识的幺半群。结果,Maybe String也是一个幺半群。
> Just "Fo" <> Just "o"\nJust "Foo"\n> Just "Fo" <> Nothing\nJust "Fo"\nRun Code Online (Sandbox Code Playgroud)\nMaybe Int不是一个幺半群,因为Int不是一个幺半群。这是因为如何制作Int幺半群有多种选择,因此没有一个选择是特殊的。相反,使用不同的实例定义了几种新类型,Monoid以明确您要使用的操作。
> import Data.Monoid\n> (3 :: Int) <> 5\n\n<interactive>:8:1: error:\n \xe2\x80\xa2 No instance for (Semigroup Int) arising from a use of \xe2\x80\x98<>\xe2\x80\x99\n \xe2\x80\xa2 In the expression: (3 :: Int) <> 5\n In an equation for \xe2\x80\x98it\xe2\x80\x99: it = (3 :: Int) <> 5\n\n-- (+) and 0\n> Sum (3 :: Int) <> Sum 5\nSum {getSum = 8}\n\n-- (*) and 1\n> Product (3 :: Int) <> Product 5\nProduct {getProduct = 15}\n\n> Just (Sum (3 :: Int)) <> Just (Sum 5)\nJust (Sum {getSum = 8})\n> Just (Sum (3 :: Int)) <> Nothing\nJust (Sum {getSum = 3})\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
229 次 |
| 最近记录: |