如何从Haskell中的IO操作获得正常值 [英] How to get normal value from IO action in Haskell

查看:82
本文介绍了如何从Haskell中的IO操作获得正常值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

  get :: Chars  - > IO chars 
get cs = do
char < - getChar
let(dats,idx)=(curData cs,curIndex cs)
let(x,y:xs)= splitAt idx dats
let replacement = x ++(ord char):xs
return $ Chars替换idx

,我希望获得 Chars ,而不是 IO操作。我有 no 想法如何做到这一点,或者甚至可能。



Chars基本上只是一个包含 [Int] 称为curData,一个 Int 称为curIndex。具体细节并不重要,我只想知道是否有一种方法可以返回 Chars 而不是 IO Chars code $。

如果不是,我如何将它作为参数传递给一个函数,该函数需要 Chars ?我对Haskell IO很陌生,但我不认为我希望所有使用 Chars 作为参数的函数必须采用 IO Chars 作为参数,然后提取并重新打包它们。这看起来没有必要。



谢谢!

解决方案

,因为这会违反参照透明度



Haskell中的IO正是用这种方式来区分行为的结果和效果可能会因环境/用户和纯函数的交互而​​变化,而纯函数的结果在您调用它们时不会改变输入参数。

为了将结果传递给一个纯函数,在输入中输入 Chars ,您必须调用你的IO动作放到另一个IO动作中,将结果与< - 运算符绑定到一个变量并传递给你的纯函数。伪代码示例:

  myPureFunction :: Chars  - > ... 

otherAction ::字符 - > IO()
otherAction cs = do
myChars< - get cs
let pureResult = myPureFunction myChars
...

如果您是haskell中的IO新手,您可能希望查看了解你对Haskell的好处!真实世界的哈斯克尔



实际上有一种方法可以简单地从IO动作中获得纯价值,但就您的情况而言你不应该这样做,因为你正在与环境进行交互:只有当你可以保证你没有违反参考透明度时,不安全的方式才是好的。


I have the following function:

get :: Chars -> IO Chars
get cs = do
    char <- getChar
    let (dats, idx) = (curData cs, curIndex cs)
    let (x,y:xs) = splitAt idx dats
    let replacement = x ++ (ord char) : xs
    return $ Chars replacement idx

and I'm wanting to get a Chars out of it, not an IO action. I have no idea how to do this, or if it is even possible.

Chars is basically just a container with an [Int] called curData and an Int called curIndex. The specifics aren't that important, I just want to know if there's a way for this function to return a Chars instead of an IO Chars.

If not, how do I pass this as an argument to a function that takes a Chars? I'm kind of new to Haskell IO, but I don't think I want all of my functions that take Chars as arguments to instead have to take IO Chars as arguments, and then extract and repackage them. It seems unnecessary.

Thanks!

解决方案

You can't, because that would violate referential transparency.

IO in Haskell is made this way exactly to distinguish between actions whose result and effects may vary depending on the interaction with the environment/user and pure functions whose results are not going to change when you call them with the same input parameters.

In order to pass the result to a pure function taking a Chars in input you have to call your IO action into another IO action, bind the result with the <- operator to a variable and pass it to your pure function. Pseudocode example:

myPureFunction :: Chars -> ...

otherAction :: Chars -> IO ()
otherAction cs = do
  myChars <- get cs
  let pureResult = myPureFunction myChars
  ...

If you're new to IO in haskell, you may wish to have a look at the Input and Output chapters in Learn You a Haskell for a Great Good! and Real World Haskell.

There is actually a way to simply get a pure value out of an IO action, but in your case you shouldn't do it, as you're interacting with the environment: the unsafe way is ok only when you can guarantee you're not violating referential transparency.

这篇关于如何从Haskell中的IO操作获得正常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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