;; Exam Question #2 (define (test-let) (define (f) (display "woof") 7) (let ((n (f))) (+ n 5) (+ n 2))) ; call by running: ; (test-let) (define (test-define) (define (f) (display "woof") 7) (+ (f) 5) (+ (f) 2)) ; call by running ; (test-define) ;; Exam Question #3 (define (f g) (g 2)) (define square (lambda (x) (* x x))) (f square) (f (lambda (z) (* z (+ z 1)))) ;(f f) ; note that you can see what is going on by ; (trace f) before typing (f f) ;; Exam Question #4 ;; Part Constructor takes an ID number, a price, ;; and a location-bin number and creates a part ;; the id number and bin number are integers ;; the price is a real number (define (make-part id price location) (list id price location)) ;; selector functions take a part and get its pieces (define (get-id part) (car part)) (define (get-price part) (car (cdr part))) (define (get-location part) (car (cdr (cdr part)))) ;; The following can be used for testing both ;; question 4 and question 5 ;; note some of these test parts are the same ;; even though they have different names so that ;; you can test equal-part? (define test-part (make-part '123 '17.26 '47 )) (define test-part-2 (make-part'456 '5.25 '23)) (define test-part-3 (make-part '123 '17.26 '47)) (define test-part-4 (make-part'456 '5.25 '23)) (define test-part-5 (make-part '789 '17.26 '47)) (define test-part-6 (make-part '765 '17.26 '19)) ; note test-part-2 and test-part-4 are equal-part? (define p-list-1 (list test-part test-part-2)) (define p-list-2 (list test-part-4 test-part-5 test-part-6)) ;; Question 5 -- Union ; takes two parts and returns #t if they are the ; same -- i.e., if every field is the same (define (equal-part? p1 p2) (and (equal? (get-id p1) (get-id p2)) (equal? (get-price p1) (get-price p2)) (equal? (get-location p1) (get-location p2)))) ; takes two lists of parts and returs their union ; assume that neither part-list-a nor part-list-b ; have any duplicates (define (union part-list-a part-list-b) (cond ((null? part-list-a) part-list-b) ((part-list-member? (car part-list-a) part-list-b) (union (cdr part-list-a) part-list-b)) (else (cons (car part-list-a) (union (cdr part-list-a) part-list-b))))) ; takes a part and a list of parts and returns ; non-#f if part is a member of part-list (define (part-list-member? part part-list) (cond ((null? part-list) #f) ((equal-part? part (car part-list)) part-list) (else (part-list-member? part (cdr part-list))))) ; takes two lists of parts (containing no duplicates) ; and returns the union -- i.e., all elements of both ; lists with no duplicates ; ; different from above in that is uses map instead ; of a helping function ;; NOTE: must use eval which has not been covered ;; it evaulates its arguments and then evalutes them ;; again (define (map-union pl1 pl2) (cond ((null? pl1) pl2) ((eval (cons 'or (map (lambda (x) (equal-part? x (car pl1))) pl2))) (map-union (cdr pl1) pl2)) (else (cons (car pl1) (map-union (cdr pl1) pl2))))) ; returns a list containing those ; elements of lst that return non-#f for ; test (define (filter test lst) (cond ((null? lst) ()) ((test (car lst)) (cons (car lst) (filter test (cdr lst)))) (else (filter test (cdr lst))))) ;; Define union of two lists of parts using filter ;; not as efficient as it could be (define (filter-union pl1 pl2) (remove-duplicate-parts (append pl1 pl2))) ; takes a list of parts and returns a list ; after removing all duplictes (define (remove-duplicate-parts lst) (if (null? lst) () (cons (car lst) (remove-duplicate-parts (filter (lambda (x) (not (equal-part? x (car lst)))) lst))))) ;; Question #7 ; takes an integer n and displays that many ; *'s followed by a newline; returns the value n (define (spots n) (cond ((equal? n 0) (newline) n) (else (display "*") (spots (- n 1)) n))) ; takes a function of 1 argument that ;returns positive integers, a starting point ;and the number of points desired to be plotted. ;Plots fn(start)...fn(start+n-1) (define (plot fn start n) (cond ((equal? n 1) (spots (fn start)) #t) (else (spots (fn start)) (plot fn (+ 1 start) (- n 1))))) ; takes a function of 1 argument that ;returns positive integers, a starting point ;and the number of points desired to be plotted. ;Plots fn(start)...fn(start+n-1) ;; iterative version (define (iplot fn start n) (define (iter-plot k) (cond ((= n k) 0) (else (spots (fn (+ start k))) (iter-plot (+ k 1))))) (iter-plot 0))