从monad转换为适用性 [英] Translate from monad to applicative

查看:68
本文介绍了从monad转换为适用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我知道Applicative类型类包含的内容,以及为什么这样做有用.但是在一个不平凡的例子中,我不太了解您如何使用它.

OK, so I know what the Applicative type class contains, and why that's useful. But I can't quite wrap my brain around how you'd use it in a non-trivial example.

例如,考虑以下相当简单的Parsec解析器:

Consider, for example, the following fairly simple Parsec parser:

integer :: Parser Integer
integer = do
  many1 space
  ds <- many1 digit
  return $ read ds

现在,如果不对Parser使用Monad实例,您将如何写呢?很多人认为这是可以做到的,是个好主意,但我不知道具体如何.

Now how the heck would you write that without using the Monad instance for Parser? Lots of people claim that this can be done and is a good idea, but I can't figure out how exactly.

推荐答案

integer :: Parser Integer
integer = read <$> (many1 space *> many1 digit)

integer = const read <$> many1 space <*> many1 digit

您是否认为这两种方法更具可读性取决于您自己.

Whether you think either of these are more readable is up to you.

这篇关于从monad转换为适用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆