OCaml函数评估算术表达式 [英] OCaml function to evaluate arithmetic expressions

查看:150
本文介绍了OCaml函数评估算术表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了数学计算器程序的一部分.我已经完成了程序的解析器, 因此,当用户以字符串形式输入数学表达式时,我已经有了一种获取方法 相应的以下类型的数据结构:

I’ve written part of a mathematical calculator program. I’ve finished the parser of the program, so when a user types in a mathematical expression as a string, I already have a way to obtain a corresponding data structure of the following type:

type expression =
| Term of int
| Addition of expression * expression
| Multiplication of expression * expression
| Subtraction of expression * expression

我现在需要的是一种评估这类数据结构的方法.

All I need now is a way to evaluate these kinds of data structures.

  • (a)编写与数学表达式"3 *((1 + 4)-5)相对应的表达式".
  • (b)扩展Ocaml数据类型以包括一个阶乘构造函数,以便它可以处理 诸如"3 *((1 + 4)!-5)."
  • (c)编写一个Ocaml函数eval,该eval接受一个数学表达式并返回其 (整数)值.例如,它应部分返回表达式的结果,返回0 (a).

  • (a) Write the expression corresponding to the mathematical statement "3*((1 + 4)- 5)."
  • (b) Extend the Ocaml datatype to include a Factorial constructor, so that it can handle expressions like "3*((1 + 4)! -5)."
  • (c) Write an Ocaml function eval which takes a mathematical expression and returns its (integer) value. For example, it should return 0 as the result from the expression in part (a).

#let rec eval expr = ... val eval:表达式-> int =

# let rec eval expr = ... val eval : expression -> int =

您可能想编写一个阶乘Ocaml函数来帮助新的阶乘 构造函数.

You may want to write a factorial Ocaml function to help out with the new Factorial constructor.

我对a部分感到困惑.是let expression = 3*((1 + 4)- 5);;吗?

I'm confused about part a. Does it mean let expression = 3*((1 + 4)- 5);;?

对于b部分,我应该遵循模式|Factorial of expression * expression吗?

For part b, should I follow the pattern |Factorial of expression * expression?

推荐答案

a)的含义是,您现在可以将数学表达式编写为提供的数据类型expression的值.例如,构造函数Addition表示加法运算符(通常写为(+))通过将两个较小的表达式作为操作数来形成表达式.因此,例如,数学表达式2 + 3可以用Caml值表示

What is meant in a) is that you can now write mathematical expressions as values of the provided datatype expression. For example, the constructor Addition expresses that the addition operator (normally written as (+)) forms an expression by taking two smaller expressions as operands. So, for instance, the mathematical expression 2 + 3 can be represented by the Caml value

Addition (Term 2,Term 3)

现在,观察到阶乘运算符采用一个参数而不是两个参数,您应该能够使用b)中要求的阶乘构造函数扩展数据类型expression.

Now, observing that the factorial operator takes one argument rather than two arguments, you should be able to extend the datatype expression with a constructor for factorials as is asked in b).

最后,在c)中,为这种类型的表达式类型编写评估函数非常简单:常量只是映射到它们所携带的整数值,而评估加法则涉及递归评估加法的操作数,然后将它们相加:

Finally, in c), writing an evaluation function for this kind of expression types is straightforward: constants just map to the integer value they are carrying and evaluating an addition involves recursively evaluating the operands of the addition and then adding them up:

let rec eval = function
  | Term n -> n
  | Addition (l,r) -> eval e1 + eval e2
  | ...

其余的情况(对于其余的构造函数,包括将要为b添加的阶乘的构造函数的一种情况)是类似的,您现在应该可以自己进行操作了.

The remaining cases (for the remaining constructors, including the one for the constructor for factorials that you will add for b)) are analogous and you should be able to do that by yourself now.

祝你好运!

这篇关于OCaml函数评估算术表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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