Docsity

Homework 2 Questions for Programming Languages | CS 3723, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 2 (20pts)
due electronically by the end of Feb 4, 2009
The homework requires you to solve the following problems via scheme programming. You need
to develop your scheme programs using the DrScheme environment and submit your solution file
electronically at
www.cs.utsa.edu/~cs3723
1. Define a function P ostOrder V isitBtr ee, which takes two parameters, a function fand a
binary tree data structure implemented using Scheme lists (that is, each list in the binary tree
has exactly three components: the parent (which must be an atomic value), the left child, and
the right child). The function returns a new binary tree that contains the result of applying
fto each subtree encounted during the post-order traversal of x. In particular, the function
parameter fshould be invoked with three parameters, the parent of the subtree to operate
on, the result of operating on the left-subtree, and the result of operating on the right subtree.
Use the following examples to test your function (include these tests in your solution file).
(define Btree (lambda (parent left right)
(if (and (null? left) (null? right)) parent
(cons parent (cons left (cons right โ€™()))))))
(PostOrderVisitBtree Btree โ€™())
(PostOrderVisitBtree Btree 7)
(PostOrderVisitBtree Btree โ€™(+ 3 5))
(PostOrderVisitBtree Btree โ€™(+ (* 3 x) (- 5 y)))
(PostOrderVisitBtree Btree โ€™(+ (* 3 x) (- 5 (/ 2 y))))
(define Op (lambda (x) (cond ((eq? x โ€™+) +)
((eq? x โ€™-) -)
((eq? x โ€™*) *)
((eq? x โ€™/) /)
(else x)
)))
(define Eval2
(lambda (parent left right)
(if (and (number? left) (number? right))
((Op parent) left right)
(Btree parent left right))))
(PostOrderVisitBtree Eval2 โ€™())
(PostOrderVisitBtree Eval2 โ€™(+ 3 5))
(PostOrderVisitBtree Eval2 โ€™(+ (* 3 x) (+ 5 2)))
(PostOrderVisitBtree Eval2 โ€™(+ (* 3 2) (- 5 (/ 108 2))))
The output from the above tests should be
1
pf2

Partial preview of the text

Download Homework 2 Questions for Programming Languages | CS 3723 and more Assignments Programming Languages in PDF only on Docsity!

Homework 2 (20pts)

due electronically by the end of Feb 4, 2009

The homework requires you to solve the following problems via scheme programming. You need to develop your scheme programs using the DrScheme environment and submit your solution file electronically at

www.cs.utsa.edu/~cs

  1. Define a function P ostOrderV isitBtree, which takes two parameters, a function f and a binary tree data structure implemented using Scheme lists (that is, each list in the binary tree has exactly three components: the parent (which must be an atomic value), the left child, and the right child). The function returns a new binary tree that contains the result of applying f to each subtree encounted during the post-order traversal of x. In particular, the function parameter f should be invoked with three parameters, the parent of the subtree to operate on, the result of operating on the left-subtree, and the result of operating on the right subtree. Use the following examples to test your function (include these tests in your solution file).

(define Btree (lambda (parent left right) (if (and (null? left) (null? right)) parent (cons parent (cons left (cons right โ€™())))))) (PostOrderVisitBtree Btree โ€™()) (PostOrderVisitBtree Btree 7) (PostOrderVisitBtree Btree โ€™(+ 3 5)) (PostOrderVisitBtree Btree โ€™(+ (* 3 x) (- 5 y))) (PostOrderVisitBtree Btree โ€™(+ (* 3 x) (- 5 (/ 2 y))))

(define Op (lambda (x) (cond ((eq? x โ€™+) +) ((eq? x โ€™-) -) ((eq? x โ€™*) *) ((eq? x โ€™/) /) (else x) )))

(define Eval (lambda (parent left right) (if (and (number? left) (number? right)) ((Op parent) left right) (Btree parent left right))))

(PostOrderVisitBtree Eval2 โ€™()) (PostOrderVisitBtree Eval2 โ€™(+ 3 5)) (PostOrderVisitBtree Eval2 โ€™(+ (* 3 x) (+ 5 2))) (PostOrderVisitBtree Eval2 โ€™(+ (* 3 2) (- 5 (/ 108 2))))

The output from the above tests should be

empty 7 (list โ€™+ 3 5) (list โ€™+ (list โ€™* 3 โ€™x) (list โ€™- 5 โ€™y)) (list โ€™+ (list โ€™* 3 โ€™x) (list โ€™- 5 (list โ€™/ 2 โ€™y))) empty 8 (list โ€™+ (list โ€™* 3 โ€™x) 7)

  1. By modifying your P ostOrderV isitBtree function, build a P ostOrderV isitT ree function which works on arbitrary tree data structures. In particular, each parent in x could have an arbitrary number of children. To accommodate this, the input function parameter f should be invoked with two parameters, the parent of the subtree to operate on, and a list that con- tains the results of operating on all the children of the subtree. Use the following examples to test your program (Hint: you could use the maplist function given lab3 to help build the P ostOrderV isitT ree).

(define Tree (lambda (parent children) (if (null? children) parent (cons parent children))))

(define reduce (lambda (f0 f1 f2 x) (cond ((null? x) f0) (else (f2 (f1 (car x)) (reduce f0 f1 f2 (cdr x))))))) (define Eval (lambda (parent children) (if (and (cons? children) (reduce true number? (lambda (x y) (and x y)) childre (reduce 0 (lambda (x) x) (Op parent) children) (Tree parent children))))

(PostOrderVisitTree Tree โ€™()) (PostOrderVisitTree Tree 7) (PostOrderVisitTree Tree โ€™(+ 3 5 7 9)) (PostOrderVisitTree Tree โ€™(+ (* 3 x z) (- 5 y x))) (PostOrderVisitTree Tree โ€™(+ (* 3 x 7) (- 5 (/ 2 y 9))))

(PostOrderVisitTree Eval โ€™()) (PostOrderVisitTree Eval 7) (PostOrderVisitTree Eval โ€™(+ 3 5 7 9)) (PostOrderVisitTree Eval โ€™(+ (* 3 x z) (- 5 y x))) (PostOrderVisitTree Eval โ€™(+ (* 3 x 7) (- 5 (/ 2 y 9))))