Why ZipList is not the default Applicative Instance for List

做~自己de王妃 提交于 2020-01-02 05:02:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37627513/why-ziplist-is-not-the-default-applicative-instance-for-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!