Project 1b

Due Date: Wed 29 Sep

Part 1: Coding

  • First define a discriminated union type for representing simple arithmetic expressions consisting of the following elements:
    • integer constants
    • binary operators: addition, subtraction, multiplication, and division
    • unary operators: negation (unary minus)
  • Your type should support expressions of arbitrary depth. There should be a separate branch for each operator.
  • Define several sample expressions.
  • Then define an evaluate function that evaluates such expressions like a calculator.
  • Test your function using the sample expressions.
  • Move the sample expressions into a suitable FsUnit test case for evaluate.
  • Now define a size function that computes the number of all nodes (operators and constants) in an expression.
  • Test your function using the sample expressions.
  • Move the sample expressions into a suitable FsUnit test case for size.
For guidance, please take a look at the org chart example on the mailing list.

Part 2: Writing

In 300-500 words, reflect on your experience from part 1 and compare it to the way you would have solved the same problem in a mainstream object-oriented language such as Java or C#.

Examples

> size (Plus(Const 3, Plus(Const 5, Plus(Const 3, Const 4))));;
(* 3 + (5 + (3 + 4))) *)
val it : int = 7

> eval (Plus(Const 3, Plus(Const 5, Plus(Const 3, Const 4))));;
val it : int = 15

> eval (Plus(Const 3, Plus(Const 5, Plus(Neg (Const 3), Const 4))));;
(* 3 + (5 + ((- 3) + 4))) *)
val it : int = 9
Comments