←back to thread

A list is a monad

(alexyorke.github.io)
153 points polygot | 1 comments | | HN request time: 0.206s | source
1. 1-more ◴[] No.44445847[source]
IDK if it ads anything to the article, but `map` is a property of Functors, and every Monad is a Functor. Well, every Monad is an Applicative Functor, and every applicative functor is a functor.

All a way of saying that, yep, you always have `map` when you have a Monad, but you don't need a Monad to have `map`.

If you want an example we can compare a regular list and a Ziplist. A regular list's Applicative instance does a cross product, while a Ziplist's applicative instance does a dot product.

    (*) <$> [2,3] <*> [5,7, 11]
    --> [10,14,22,15,21,33] 

    getZipList $ fmap show $ (*) <$> ZipList [2,3] <*> ZipList [5,7, 11]
    --> ["10","21"]  
There's no great way to write a Monad instance for ZipList. But it's an Applicative Functor and thus is also a Functor and thus you can map over it. https://www.mail-archive.com/haskell-cafe@haskell.org/msg572...

For quirky reasons in Haskell, `fmap` the function implemented for every Functor instance. This is because `map` was already taken by lists. Weird, I know.