如何评估表达式?

发布于 2024-07-10 14:11:01 字数 209 浏览 6 评论 0原文

给定表达式所包含变量的值列表,如何计算表达式?

eval::[(Variable,Integer)]->Expr->Integer

例子:

eval[("x",2), ("y",4)](Mult(Plus(Var "x") (Const))(Var "y"))= 12

How can I evaluate an expression, given a list of values for the variables it contains?

eval::[(Variable,Integer)]->Expr->Integer

Example:

eval[("x",2), ("y",4)](Mult(Plus(Var "x") (Const))(Var "y"))= 12

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长不大的小祸害 2024-07-17 14:11:01

Variable 和 Expr 不是 Haskell 中的内置类型。

如果您正在使用一个库或正在开发一个较大程序的一部分,那么它可能具有您想要的功能。

如果您自己定义了这些类型,那么这取决于您。

如果这是为了课程作业,那么您可能想阅读有关语法和解析技术的内容。 尝试将字符串分解为标记并构建可以评估的符号表示。 如果您还没有找到它 http://www.zvon.org/other/ haskell/Outputglobal/index.html 是一个很好的参考站点。

如果您喜欢更重量级的东西(并且牢牢掌握 Haskell 和 Monadic 编程),那么我建议您花时间学习使用 Parsec http://www.haskell.org/haskellwiki/Parsec

Variable and Expr are not inbuild types in Haskell.

If you are using a library or working on a part of a larger program maybe it has the capabilities you are after.

If you have defined these types yourself, then its up to you.

If this is for coursework, then you might want to read about grammers and parsing techniques. Try breaking the string up into tokens and constructing a symbolic representation you can evaluate. If you havent found it already http://www.zvon.org/other/haskell/Outputglobal/index.html is a good reference site.

If your into something more heavyweight (and have a firm grip on haskell and monadic programming) then I'd reccomend investing the time in learning to use Parsec http://www.haskell.org/haskellwiki/Parsec.

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