如何使用原始递归简化以下表达式?

发布于 2024-07-09 16:04:52 字数 515 浏览 8 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

和我恋爱吧 2024-07-16 16:04:52

那么,您不能将模式匹配应用于个别案例吗?

simplify (Plus (Const 0) (Expr x)) = simplify (Expr x)
simplify (Plus (Expr x) (Const 0)) = simplify (Expr x)
simplify (Mult (Const 0) _) = Const 0
simplify (Mult _ (Const 0)) = Const 0
– … and so on

编辑:是的,当然……添加了递归。

Well, can't you apply pattern matching to the individual cases?

simplify (Plus (Const 0) (Expr x)) = simplify (Expr x)
simplify (Plus (Expr x) (Const 0)) = simplify (Expr x)
simplify (Mult (Const 0) _) = Const 0
simplify (Mult _ (Const 0)) = Const 0
– … and so on

EDIT: Yes, of course … recursion added.

|煩躁 2024-07-16 16:04:52

我对 Haskell 不太了解,但本质上你会想要进行表达式树遍历。

这棵树是
EXP:(操作员)(EXP)(EXP)
经验:(常量)
EXP: (var)

那么你的简化就变成了
这是伪代码,

simplify(Exp e)
if (e is const) return e
else if (e is var) return e
else
{//encode simplification rules
    Exp left = simplify(e.left)
    Exp right = simplify(e.right)
    if(operator is PLUS)
    {
        if(left == 0) return right;
        if(right == 0) return left;
    }
    else if(operator is MULT)
    {
        if(left == 1) return right;
        if(right == 1) return left;
        if(left == 0) return 0;
        if(right == 0) return 0;
    }
//and so on for other operators
} 

有点像 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

simplify(Exp e)
if (e is const) return e
else if (e is var) return e
else
{//encode simplification rules
    Exp left = simplify(e.left)
    Exp right = simplify(e.right)
    if(operator is PLUS)
    {
        if(left == 0) return right;
        if(right == 0) return left;
    }
    else if(operator is MULT)
    {
        if(left == 1) return right;
        if(right == 1) return left;
        if(left == 0) return 0;
        if(right == 0) return 0;
    }
//and so on for other operators
} 

this is sort of java esque but i think the idea is there, essentially youre going to have to do a tree traversal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文