Docsity

Homework 4 Problems - Programming Languages | CS 3723, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 4 (30pts)
Due Oct 22, 2008
Please submit this homework electronically to [email protected].
1. (15pts) Translate the following Scheme code to an equivalent function definition in
ML.
(define sum (lambda (y)
(cond ((null? y) 0)
((cons? y) (+ (car y) (sum (cdr y)))))))
2. (15pts) Translate the following AST-processing Scheme function to ML.
(define EvalExp (lambda (exp)
(cond ((number? exp) exp)
((eq? (car exp) โ€™+)
(+ (EvalExp (cadr exp)) (EvalExp (caddr exp))))
((eq? (car exp) โ€™-)
(- (EvalExp (cadr exp)) (EvalExp (caddr exp)))))))
To see how the Scheme function work, you can test the given function in DrScheme
with the following test cases
> (EvalExp โ€™(+ (- 5 1) (+ 2 7)))
13
> (EvalExp โ€™15)
15
> (EvalExp โ€™(- (+ 1 9) 5))
5
>
Your translation should use the following datatype definition.
datatype AST = Number of int | Plus of AST * AST | Minus of AST * AST;
1

Partial preview of the text

Download Homework 4 Problems - Programming Languages | CS 3723 and more Assignments Programming Languages in PDF only on Docsity!

Homework 4 (30pts)

Due Oct 22, 2008

Please submit this homework electronically to [email protected].

  1. (15pts) Translate the following Scheme code to an equivalent function definition in ML.

(define sum (lambda (y) (cond ((null? y) 0) ((cons? y) (+ (car y) (sum (cdr y)))))))

  1. (15pts) Translate the following AST-processing Scheme function to ML.

(define EvalExp (lambda (exp) (cond ((number? exp) exp) ((eq? (car exp) โ€™+) (+ (EvalExp (cadr exp)) (EvalExp (caddr exp)))) ((eq? (car exp) โ€™-) (- (EvalExp (cadr exp)) (EvalExp (caddr exp)))))))

To see how the Scheme function work, you can test the given function in DrScheme with the following test cases

(EvalExp โ€™(+ (- 5 1) (+ 2 7))) 13 (EvalExp โ€™15) 15 (EvalExp โ€™(- (+ 1 9) 5)) 5

Your translation should use the following datatype definition.

datatype AST = Number of int | Plus of AST * AST | Minus of AST * AST;