将IO字符串转换为字符串 [英] transforming IO String to String
问题描述
我将IO String()转换为String()
的问题下面是评估表达式的函数。
foobar :: String - > IO字符串
eval :: String - > Sh()()
eval x = do
s< - foobar x
shellPutStrLn $ s
这不起作用,因为eval会返回IO String而不是Sh。
时刻某个IO在一个函数中完成,它将它转换/将其变为
IO字符串。
如何防止转换或将IO字符串转换为字符串?
它看起来像 Sh
type应该可以做IO。实现这一点的通常方式是使用monad变压器。然后你会得到:
pre $ 实例MonadIO Sh其中 - 由于您不显示Sh
$ b而定义消失$ b shellPutStrLn :: String - > Sh()
shellPutStrLn = liftIO。 putStrLn
eval :: String - > Sh()
eval x = do
s< - liftIO $ foobar x
shellPutStrLn s
请参阅 MTL 了解您可以使用的许多现成monad变压器,和 xmonad 为这种风格的一个很好的例子。
I am having an issue converting IO String() to a String() Below is the function to eval an expression.
foobar :: String -> IO String
eval :: String -> Sh () ()
eval x = do
s <- foobar x
shellPutStrLn $ s
This isnt working, because eval returns IO String instead of Sh. The moment some IO is done within a function it transforms/taints it into IO String. How do I prevent the transformation or convert an IO String to String ?
It looks like your Sh
type is supposed to be able to do IO. The usual way to implement this is with monad transformers. Then you would have:
instance MonadIO Sh where -- definition elided because you don't show Sh
shellPutStrLn :: String -> Sh ()
shellPutStrLn = liftIO . putStrLn
eval :: String -> Sh ()
eval x = do
s <- liftIO $ foobar x
shellPutStrLn s
See the MTL for a lot of ready monad transformers you can use, and xmonad for a good example of this style in practice.
这篇关于将IO字符串转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!