如何从 Haskell 的 monad 中取出一个值? [英] How to take out a value out of a monad in Haskell?
问题描述
有没有办法从 monad 中取出东西"?
Is there any way to take "things" out of a monad?
我正在开发一款游戏,现在我正在尝试了解数据库.我发现 happstack
非常好,但我拿不到东西.
I am developing a game, and I am now trying to understand about databases. I found happstack
really nice, but I can't get the thing.
比如我有这个功能(希望大家熟悉happstack
)
For example, I have this function (hope you are familiar with happstack
)
getAllThings :: MonadIO m => m [Thing]
getAllThings = do
elems <- query GetThings
return elems
所以我得到 m [Things]
,但是我不能在我的模型中使用它!例如
So I get m [Things]
, but I can't use this in my model! For instance
doSomeThingWithThings :: [Thing] -> Something
我用谷歌搜索了这个,我什么也没找到.
I googled this and I found nothing.
推荐答案
您不应该以这种方式退出 IO monad(除了 unsafePerformIO
函数),但您仍然可以在其中使用您的函数:
You are not supposed to exit IO monad this way (except unsafePerformIO
function), but you can still use your function inside it:
process :: MonadIO m => m ()
process = do
elems <- getAllThings
let smth = doSomeThingWithThings elems
-- ...
这篇关于如何从 Haskell 的 monad 中取出一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!