列表的替代

现在几次,我发现自己在定义:

(<?>) :: [a] -> [a] -> [a]
[] <?> ys = ys
xs <?> _  = xs

This is an associative operation, of course, and the empty list [] is both left- and right-identity. It functions like Python's or.

It seems to me that this would make a nice (<|>), better than (++) would. Choosing the first nonempty list feels more like what I would expect from a typeclass named Alternative than concatenating lists. Admittedly, it doesn't fit MonadPlus as well, but I think that a small price to pay for salvation. We already have (++) and (<>) in the standard library; do we need another synonym, or would a new function (as far as I can tell) be more helpful?

I was at first thinking this might be a good Alternative instance for ZipList, but the discussion following this answer on the relevant question has convinced me otherwise. Other than backwards-compatibility and keeping MonadPlus sensible, what arguments are there for the current instance rather than this new one?

评论