支持一个变量的小型数学类型语言的评估
我已经编写了读取字符串输入的解析器。那行得通。我还编写了吐出结果的评估器。但有一个小细节我在实施时遇到了麻烦。看下面的例子:
+(sw+(2,2),sr)
这个小语言的 sw 构造应该计算“+(2,2)”并将其存储在某个地方。 sr 构造将读取该存储区域。整个表达式的计算结果为 8。
我的想法是为函数 eval 使用一个额外的参数来存储结果。但我看不出这能奏效。请注意,我是 Haskell 的新手,所以请友善。哦,这是作业。所以不要给我解决方案,给我提示。
I have written the parser that reads the string input. That works. I have also written the evaluator that spits out the result. But there is one small detail that I'm having trouble implementing. Look at the following example:
+(sw+(2,2),sr)
The sw construct of this tiny language is suppose to evaluate "+(2,2)" and store it somewhere. The sr construct would read this storage area. The whole expression over would evaluate to 8.
My thoughts on it would be to use an extra parameter for the function eval, that stores the result. But I can't see that working out. Note I am new to haskell, so be kind. Oh, this is homework. So don't give me a solution, give me a hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于表达式可以读取和写入存储区域,因此您的评估函数应该获取内存状态作为参数,并在结果中返回新状态。
[其中
Int
是存储类型,Float
是结果类型,当然你可以更改]。在实现
evaluate(Sum ab)
时,需要将内存传递给evaluate a
,获取新的内存值并将其交给evaluate b
。使用模式匹配。您可以从
let (x,m') =valuate am in ...
开始。Since expressions can read and write into storage area, your evaluation function should get state of memory as a parameter, and return new state in result.
[where
Int
is type of the storage, andFloat
is type of result, of course you can change that].When implementing
evaluate (Sum a b)
, you need to pass memory intoevaluate a
, get new value of memory and give it toevaluate b
.Use pattern matching. You'd start with
let (x,m') = evaluate a m in ...
.