Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ZipList is not the default Applicative Instance for List

I am currently learning Applicatives in Haskell. If I am not wrong, there are two different Applicative instances for Lists, (List and ZipList - the second being defined as a newtype wrapping a List value). The ZipList applicative instances seems more intuitive for me.

It might be a dumb question, but is there a specific reason ZipList is not the default Applicative instance for Lists.

pure (+) <*> [1,2,3] <*> [4,5,6]
-- [5,6,7,6,7,8,7,8,9]

pure (+) <*> ZipList [1,2,3] <*> ZipList [4,5,6]
-- ZipList [5,7,9]

Is it because the distributive version of Applicative List also happens to have a Monad instance?

like image 273
zeronone Avatar asked Jun 04 '16 07:06

zeronone


1 Answers

I suspect the honest answer here is "historical accident". The monad abstraction was popular before the applicative one was; since there is a natural list monad, that instance was defined. For backwards compatibility, it makes sense to keep the functor and applicative instances consistent moving forward.

That said, if we had to make the choice again today, I think I would support the current situation anyway: the nondeterminism monad is useful as a syntactically cheap backtracking search, and I've taken advantage of that frequently; arbitrary-arity zipping is (in my personal experience) a much less frequent need.

like image 169
Daniel Wagner Avatar answered Sep 28 '22 12:09

Daniel Wagner