哈斯克尔:电梯vs电梯 [英] Haskell: lift vs liftIO

查看:96
本文介绍了哈斯克尔:电梯vs电梯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下应该使用 liftIO ?当我使用 ErrorT String IO 时, lift 函数可以将IO动作提升为 ErrorT ,所以 liftIO 看起来多余。

In what situations should liftIO be used? When I'm using ErrorT String IO, the lift function works to lift IO actions into ErrorT, so liftIO seems superfluous.

推荐答案

lift 总是从上一个图层抬起。如果您需要从第二层提升,则需要 lift。电梯等等。

lift always lifts from the "previous" layer. If you need to lift from the second layer, you would need lift . lift and so on.

另一方面, liftIO 总是从IO层(当存在时,总是在堆栈的底部)。所以,如果你有两层以上的monads,你会明白 liftIO

On the other hand, liftIO always lifts from the IO layer (which, when present, is always on the bottom of the stack). So, if you have more than 2 layers of monads, you will appreciate liftIO.

比较参数在以下lambda中:

Compare the type of the argument in the following lambdas:

type T = ReaderT Int (WriterT String IO) Bool

> :t \x -> (lift x :: T)
\x -> (lift x :: T) :: WriterT String IO Bool -> T

> :t \x -> (liftIO x :: T)
\x -> (liftIO x :: T) :: IO Bool -> T

这篇关于哈斯克尔:电梯vs电梯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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