如何从 Haskell 的 monad 中取出一个值? [英] How to take out a value out of a monad in Haskell?

查看:26
本文介绍了如何从 Haskell 的 monad 中取出一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 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屋!

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