Docsity

Simple Arithmetic Expressions in Programming Language - Assignment 2 | COSC 4780, Assignments of Computer Science

Toggle sidebar
bg1
HW 2 Prof. Caldwell
Due: 20 January 2009 COSC 4780
Read pages 31 - 42 of the Hickey notes.
Assignment:
The following ML type describes simple arithmetic expressions.
type exp =
Var of string
| Const of int
| UMinus of exp
| Add of exp * exp
| Times of exp * exp
;;
Var,Const,UMinus,Add and Times are constructors for terms of type exp. The constructor
Var expects a string argument, Const expects an integer argument, UMinus expects an argument of
type exp (an expression) and while Add and Times both expect a pair of expressions as arguments.
The table below gives some expressions in both the standard mathematical notation and as terms
of type expression.
standard As terms in the ML type exp
notation
5Const 5
xVar "x"
(5+x) Plus(Const 5,Var "x")
(x+5) Plus(Var "x",Const 5)
(5*x) Times(Const 5, Var "x")
((5*x)+7) Plus(Times(Const 5,Var "x"),Const 7)
((5*x)/7) Div(Times(Const 5,Var "x"),Const 7)
(((5*x)-1)/7) Div(Sub(Times(Const 5,Var"x"), Const 1),Const 7)
The meaning of an expression is the integer value the expression evaluates to. Variables cause
a problem here – we must add an argument to the semantic function to carry an assignment i.e.
a function mapping strings to integer values. Assignments are used in the meaning function to
determine the (integer) value of a variable.
The following ML code implements the semantic function.
let rec meaning of exp e a =
match e with
Var(s) -> a s
| Const(i) -> i
| UMinus(e1) -> - (meaning of exp e1 a)
| Add(e1,e2) -> (meaning of exp e1 a) + (meaning of exp e2 a)
| Times(e1,e2) -> (meaning of exp e1 a) * (meaning of exp e2 a)
;;
Notice that when you enter this definition, ML figures out on its own that the argument amust
be of type string -> int.
1
pf2

Partial preview of the text

Download Simple Arithmetic Expressions in Programming Language - Assignment 2 | COSC 4780 and more Assignments Computer Science in PDF only on Docsity!

HW 2 Prof. Caldwell Due: 20 January 2009 COSC 4780

Read pages 31 - 42 of the Hickey notes. Assignment: The following ML type describes simple arithmetic expressions.

type exp = Var of string | Const of int | UMinus of exp | Add of exp * exp | Times of exp * exp ;;

Var, Const, UMinus, Add and Times are constructors for terms of type exp. The constructor Var expects a string argument, Const expects an integer argument, UMinus expects an argument of type exp (an expression) and while Add and Times both expect a pair of expressions as arguments. The table below gives some expressions in both the standard mathematical notation and as terms of type expression.

standard As terms in the ML type exp notation 5 Const 5 x Var "x" (5+x) Plus(Const 5,Var "x") (x+5) Plus(Var "x",Const 5) (5x) Times(Const 5, Var "x") ((5x)+7) Plus(Times(Const 5,Var "x"),Const 7) ((5x)/7) Div(Times(Const 5,Var "x"),Const 7) (((5x)-1)/7) Div(Sub(Times(Const 5,Var"x"), Const 1),Const 7)

The meaning of an expression is the integer value the expression evaluates to. Variables cause a problem here – we must add an argument to the semantic function to carry an assignment i.e. a function mapping strings to integer values. Assignments are used in the meaning function to determine the (integer) value of a variable. The following ML code implements the semantic function.

let rec meaning of exp e a = match e with Var(s) -> a s | Const(i) -> i | UMinus(e1) -> - (meaning of exp e1 a) | Add(e1,e2) -> (meaning of exp e1 a) + (meaning of exp e2 a) | Times(e1,e2) -> (meaning of exp e1 a) * (meaning of exp e2 a) ;;

Notice that when you enter this definition, ML figures out on its own that the argument a must be of type string -> int.

Your assignment is to modify the expression type to include operations of integer subtraction and division (use constructor names Sub and Div) and then to extend the calculator to perform these new operations as expected. Code is included on the web-page.