haskell – 在IO中提取Maybe值

栏目: 编程语言 · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/8540999/extracting-a-maybe-value-in-io

给出以下内容:

> (liftM2 fromMaybe) (ioError $userError "OOPS") (return $Just "ok")

ghci给我

*** Exception: user error (OOPS)

当然,从Mayay可以正常工作:

> (liftM2 fromMaybe) (return $"not me") (return $Just "ok")
"ok"

但是,IO操作似乎正在执行,然后被丢弃:

> (liftM2 fromMaybe) (putStrLn "computing.." >> "discarded") (return $Just "ok")
computing..
"ok"

为什么会发生这种情况?有没有办法使IO monad lazier?

具体来说,给定值:: IO(也许a)一个(干净,简洁)的方式

result <- (liftM2 fromMaybe) err value

并解压缩结果或相应地引发IOError?

我不知道制作IO懒惰是这里的正确方向.你似乎想做的是首先得到可能,然后消除它.这可以写几种方法,这里有一个选项:
test :: IO (Maybe a) -> IO a
test = (>>= maybe (ioError $userError "oops") return)

http://stackoverflow.com/questions/8540999/extracting-a-maybe-value-in-io


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Invisible Users

Invisible Users

Jenna Burrell / The MIT Press / 2012-5-4 / USD 36.00

The urban youth frequenting the Internet cafes of Accra, Ghana, who are decidedly not members of their country's elite, use the Internet largely as a way to orchestrate encounters across distance and ......一起来看看 《Invisible Users》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码