为什么ZipList不是List的默认应用实例 [英] Why ZipList is not the default Applicative Instance for List
问题描述
我目前正在Haskell学习Applicatives.如果我没看错,则List有两个不同的Applicative实例(List
和ZipList
-第二个被定义为包装List值的新类型). ZipList
适用实例对我来说似乎更直观.
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.
这可能是一个愚蠢的问题,但是是否有特定原因导致ZipList
不是列表的默认应用实例.
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]
是因为分发版的应用列表"也恰好有一个Monad实例吗?
Is it because the distributive version of Applicative List also happens to have a Monad instance?
推荐答案
我怀疑诚实的回答是历史性事故". monad抽象在应用抽象之前就很流行.由于存在一个自然列表monad,因此定义了该实例.为了向后兼容,必须使函子和应用实例保持一致并向前发展.
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.
这篇关于为什么ZipList不是List的默认应用实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!