Haskell - What is Control.Applicative.Alternative good for?

ぐ巨炮叔叔 提交于 2019-12-18 01:58:48

问题


I was looking at the Applicative class within Haskell libraries and stumbled across Alternative.

What is this class good for? A google search did not reveal anything particularly insightful. And it seems to be completely out of place, bundled as it is with the Applicative package.

Could someone please post a possible scenario where you would use this class?


回答1:


It's commonly used with parser combinators. For example, if space is a parser combinator that matches a single whitespace character, many space would be one that would match consecutive whitespace.

I can agree that it's slightly out of place in Control.Applicative, though.




回答2:


Sometimes the <|> operator is quite useful:

foldl1 (<|>) [Nothing, Just 5, Just 3]
-- Just 5


来源:https://stackoverflow.com/questions/7203686/haskell-what-is-control-applicative-alternative-good-for

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