monoids

ZipList Monoid haskell

别等时光非礼了梦想. 提交于 2021-02-04 07:31:06
问题 The default monoid for lists in the GHC Prelude is concatenation. [1,2,3] <> [4,5,6] becomes [1,2,3] ++ [4,5,6] and thus [1,2,3,4,5,6] I want to write a ZipList Monoid instance that behaves like this: [ 1 <> 4 , 2 <> 5 , 3 <> 6 ] The result is [5,7,9] assuming I am using the sum monoid. Note this behaves like zipWith (+) Potentially it would behave like this: [ Sum 1 <> Sum 4 , Sum 2 <> Sum 5 , Sum 3 <> Sum 6 ] I need to create a newtype around the ZipList newtype and the Sum newtype in order

ZipList Monoid haskell

隐身守侯 提交于 2021-02-04 07:29:08
问题 The default monoid for lists in the GHC Prelude is concatenation. [1,2,3] <> [4,5,6] becomes [1,2,3] ++ [4,5,6] and thus [1,2,3,4,5,6] I want to write a ZipList Monoid instance that behaves like this: [ 1 <> 4 , 2 <> 5 , 3 <> 6 ] The result is [5,7,9] assuming I am using the sum monoid. Note this behaves like zipWith (+) Potentially it would behave like this: [ Sum 1 <> Sum 4 , Sum 2 <> Sum 5 , Sum 3 <> Sum 6 ] I need to create a newtype around the ZipList newtype and the Sum newtype in order

What is the main difference between Free Monoid and Monoid?

一个人想着一个人 提交于 2020-03-18 11:10:37
问题 Looks like I have a pretty clear understanding what a Monoid is in Haskell, but last time I heard about something called a free monoid. What is a free monoid and how does it relate to a monoid? Can you provide an example in Haskell? 回答1: In a programming context, I usually translate free monoid to [a] . In his excellent series of articles about category theory for programmers, Bartosz Milewski describes free monoids in Haskell as the list monoid (assuming one ignores some problems with

Higher Kinded Types in Scala [duplicate]

左心房为你撑大大i 提交于 2020-01-12 10:46:10
问题 This question already has answers here : What is a higher kinded type in Scala? (4 answers) Closed 2 years ago . I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this: trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } Later on, they define specific Monoid instances by extending this interface. For example., val intMonoid = new Monoid[Int] { ... } val listMonoid = new Monoid[List[Int]] { ... } A

Higher Kinded Types in Scala [duplicate]

拈花ヽ惹草 提交于 2020-01-12 10:45:13
问题 This question already has answers here : What is a higher kinded type in Scala? (4 answers) Closed 2 years ago . I'm reading through the Functional Programming in Scala book and in the Monoids chapter, they talk about a Monoid interface that looks like this: trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } Later on, they define specific Monoid instances by extending this interface. For example., val intMonoid = new Monoid[Int] { ... } val listMonoid = new Monoid[List[Int]] { ... } A

What's the practical value of all those newtype wrappers in `Data.Monoid`?

不想你离开。 提交于 2020-01-09 19:51:15
问题 When looking at Data.Monoid , I see there are various newtype wrappers, such as All , Sum , or Product , which encode various kinds of monoids. However, when trying to use those wrappers, I can't help but wonder what's the benefit over using their non- Data.Monoid counterparts. For instance, compare the rather cumbersome summation print $ getSum $ mconcat [ Sum 33, Sum 2, Sum 55 ] vs. the more succinct idiomatic variant print $ sum [ 33, 2, 55 ] But what's the point? Is there any practical

What's the practical value of all those newtype wrappers in `Data.Monoid`?

▼魔方 西西 提交于 2020-01-09 19:51:05
问题 When looking at Data.Monoid , I see there are various newtype wrappers, such as All , Sum , or Product , which encode various kinds of monoids. However, when trying to use those wrappers, I can't help but wonder what's the benefit over using their non- Data.Monoid counterparts. For instance, compare the rather cumbersome summation print $ getSum $ mconcat [ Sum 33, Sum 2, Sum 55 ] vs. the more succinct idiomatic variant print $ sum [ 33, 2, 55 ] But what's the point? Is there any practical