OCaml function to evaluate arithmetic expressions

前提是你 提交于 2019-12-13 06:07:56

问题


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 =

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?


回答1:


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!




回答2:


Part b: the point of the question is that a multiplication takes two numbers as input, but factorial takes only one number as input.




回答3:


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


来源:https://stackoverflow.com/questions/11978077/ocaml-function-to-evaluate-arithmetic-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!