如何将翻译带到IO monad? [英] How to bring an interpreter to the IO monad?

查看:98
本文介绍了如何将翻译带到IO monad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与在此答案中中编写的简单解释器有关

My question relates to the simple interpreter written in in this answer

如何将IO功能添加到此解释器 (第一个非Monadic版本版本)?我的意思是简单地添加一个使用putStrLn的语句.我还不太了解Haskell,但我猜您可以通过某种方式组合IO monad.有人可以指出我正确的方向吗?

How could one add IO capabilities to this interpreter (the first non monadic version)? By this I simply mean adding a statement that uses putStrLn. I'm not that well versed in Haskell yet, but I'm guessing you can just combine the IO monad somehow. Can somebody point me in the right direction?

data Stmt
  = Var := Exp                                   
  | While Exp Stmt                                               
  | Seq [Stmt]      
  | Print Exp       -- a print statement

推荐答案

您可以使您的解释器在IO monad中得到结果:

You can make your interpreter have a result in the IO monad:

exec :: Stmt -> Store -> IO Store

现在exec函数可以在IO中执行任何操作.现有操作将需要进行相应的修改.

Now the exec function can do anything in IO. The existing operations will need to be modified accordingly.

这篇关于如何将翻译带到IO monad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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