如何使用原始递归简化以下表达式?
可能的重复:
Haskell 中的符号简化(使用递归?)
我想到的简化是
0*e = e*0 = 0
1*e = e*1 = 0+e = e+0 = e-0 = e
并简化常量子表达式,例如,Plus (Const 1) (Const 2)
将变为 Const 3
。 我不希望变量(或变量和常量)被连接:Var "st"
是与 Var "s"
不同的变量。
例如 simplify(Plus (Var "x") (Const 0))= Var "x"
Possible Duplicate:
Symbolic simplification in Haskell (using recursion?)
The simplifications I have in mind are
0*e = e*0 = 0
1*e = e*1 = 0+e = e+0 = e-0 = e
and simplifying constant subexpressions, e.g. Plus (Const 1) (Const 2)
would become Const 3
. I would not expect variables (or variables and constants) to be concatenated: Var "st"
is a distinct variable from Var "s"
.
For example simplify(Plus (Var "x") (Const 0))= Var "x"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,您不能将模式匹配应用于个别案例吗?
编辑:是的,当然……添加了递归。
Well, can't you apply pattern matching to the individual cases?
EDIT: Yes, of course … recursion added.
我对 Haskell 不太了解,但本质上你会想要进行表达式树遍历。
这棵树是
EXP:(操作员)(EXP)(EXP)
经验:(常量)
EXP: (var)
那么你的简化就变成了
这是伪代码,
有点像 java 风格,但我认为这个想法就在那里,本质上你必须进行树遍历。
I don't know much about haskell, but essentially your are going to want to do an expression tree traversal.
the tree is
EXP: (operator) (EXP) (EXP)
EXP: (const)
EXP: (var)
then your simplify becomes
heres the psuedo code
this is sort of java esque but i think the idea is there, essentially youre going to have to do a tree traversal.