[Haskell-cafe] [ZipList Monad] Final answer?
Etsuji Nakai
enakai at redhat.com
Fri Oct 18 00:55:54 UTC 2013
Hi,
I found a few interesting discussions on how to construct ZipList Monad in the past (around 2009).
For example, http://www.haskell.org/pipermail/haskell-cafe/2009-April/059079.html
But there seems no final answer to it. After a good discussion with one of my Haskell friends, we finally came up with the following one:
import Control.Monad
newtype MyZipList a = MyZipList [a] deriving (Show)
getBase :: MyZipList a -> [a]
getBase (MyZipList xs) = xs
instance Monad MyZipList where
return x = MyZipList $ repeat x
m >>= f = MyZipList $ bind (getBase m) (fmap getBase f)
where
bind :: [a] -> (a -> [b]) -> [b]
bind [] f = []
bind (x:xs) f = case f x of
[] -> []
y:_ -> y : bind xs (fmap tailOrNil f)
tailOrNil :: [b] -> [b]
tailOrNil [] = []
tailOrNil (x:xs) = xs
This is fairly simple and looks to work with variable length lists. Do you think this could be the final answer?
- Etsuji
More information about the Haskell-Cafe
mailing list