Monoid for ZipList
Andrew Martin
andrew.thaddeus at gmail.com
Thu May 3 12:05:24 UTC 2018
There is another possible instance. We can instead write:
instance Semigroup a => Semigroup (ZipList a) where
(<>) = liftA2 (<>)
instance Monoid a => Monoid (ZipList a) where
mempty = pure mempty
This behaves differently, and it is also law-abiding.
On Thu, May 3, 2018 at 5:22 AM, 박신환 <ndospark320 at naver.com> wrote:
> As *instance Alternative ZipList* is defined since 4.11.0.0:
>
> *instance Alternative <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.Base.html#Alternative> ZipList <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList> where
> empty <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.Base.html#empty> = ZipList <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList> []
> ZipList <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList> xs <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319881> <|> <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.Base.html#%3C%7C%3E> ZipList <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList> ys <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319882> = ZipList <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#ZipList> (xs <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319881> ++ <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.Base.html#%2B%2B> drop <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.List.html#drop> (length <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Data.Foldable.html#length> xs <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319881>) ys <http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Applicative.html#local-6989586621679319882>*
> *)*
> It seems perfectly fine to make *Monoid* for *ZipList* as followings:
>
>
>
>
>
>
>
> *instance Semigroup a => Semigroup (ZipList a) where ZipList [] <> ZipList ys = ZipList ys ZipList xs <> ZipList [] = ZipList xs ZipList (x:xs) <> ZipList (y:ys) = ZipList (x <> y : ZipList xs <> ZipList ys)instance Semigroup a => Monoid (ZipList a) where mempty = ZipList []*
>
> Note that this semantic is similar to that of *Maybe*.
>
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
>
--
-Andrew Thaddeus Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20180503/d9e7aa06/attachment.html>
More information about the Libraries
mailing list