简体   繁体   中英

OCaml function to evaluate arithmetic expressions

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) 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 : expression -> int = <fun>

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

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

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

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)

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).

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
  | ...

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.

Good luck!

b 部分:问题的重点是乘法需要两个数字作为输入,但阶乘只需要一个数字作为输入。

here is the answers i worked out with my profesor i thought i post the solution in case some one else cam across this problem.

(a) Write the expression corresponding to the mathematical statement “3 _ ((1 + 4) ?? 5).”

# let expression = Multiplication((Term 3, Subtraction(Term 5, Addition (Term 1, Term 4))));;

(b) Extend the Ocaml datatype to include a Factorial constructor, so that it can handle expressions like “3 _ ((1 + 4)! ?? 5).”

# type expression =
| Term of int
| Addition of expression * expression
| Multiplication of expression * expression
| Subtraction  of expression * expression
| Factorial of expression;;

# let expression = Multiplication((Term 3, Subtraction(Term 5, Factorial(Addition (Term 1, Term 4)))));;

(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). You may want to write a factorial Ocaml function to help out with the new Factorial constructor.

# let rec eval expression =
  match expression with
  |Term m -> m
  |Addition(m,n) -> add(eval(m), eval(n))
  |Subtraction(m,n) -> eval(m) - eval(n)
  |Multiplication(m,n) -> eval(m) * eval(n)
  |Factorial(m)->factorial(eval(m));;

# let add(m,n) = m+n;;

# let rec factorial n =
    if n=0 then 1 else n * factorial(n - 1);;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM