Haskell:如何从IO(a)转换为a? [英] Haskell: How to convert from IO (a) to a?

查看:58
本文介绍了Haskell:如何从IO(a)转换为a?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数返回我 IO(Map.Map String Double),并且我需要 Map.Map String Double ,而没有IO.

I have a function that returns me IO (Map.Map String Double) and I need Map.Map String Double, without IO.

我试图这样做,但是失败了:不在范围内:数据构造函数"IO".

I tried to do this, but it fails with: Not in scope: data constructor `IO'.

extractIO (IO (a)) = a

推荐答案

您不能只解开IO,这就是IO的全部重点.

You can't just unwrap IO, that's the entire point of IO.

如果您有IO(Map.Map字符串双精度型),并且要处理该东西,则必须在单子上下文中完成,即

If you have an IO (Map.Map String Double) and you want to process that thing, you have to do it within monadic context, i.e.

stuff :: IO ()
stuff = do
  map <- theThingThatReturnsYourIOMap
  theThingThatNeedsYourUnwrappedMap map

如果您更详细地说明您想做什么,我们可以为您提供更详细的答案.

If you explain in more detail what you want to do, we can give you a more detailed answer.

这篇关于Haskell:如何从IO(a)转换为a?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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