;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CIS 351 Homework Sample ;; Author: Jim Royer (royer at ecs.syr.edu) ;; Author: Maxie Cat (maxie at ronron.com) (require "utils.scm") ;; utils.scm should be in the same directory as this file. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Problem 1: EOPL 1.15. ;; douple : integer * object -> list ;; (douple n x) ;; returns a list consisting of n copies of x (define douple (lambda (n x) (if (zero? n) '() (cons x (douple (- n 1) x))))) ;; tests (equal?? (douple 0 'foo) '()) (equal?? (douple 1 'foo) '(foo)) (equal?? (douple 2 'foo) '(foo foo)) (equal?? (douple 4 '(ha ha)) '((ha ha) (ha ha) (ha ha) (ha ha))) (report-unit-tests-completed 'douple) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Problem 2: EOPL 1.17. ;; down : list -> list ;; (down lst) ;; returns the result of wrapping parens around each top-level ;; element of lst. ;; For this version I use scheme's map primitive, see ;; http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_idx_642 (define down (lambda (lst) (map list lst))) ;; tests (equal?? (down '()) '()) (equal?? (down '(1 2 3)) '((1) (2) (3))) (equal?? (down '((a) (fine) (idea))) '(((a)) ((fine)) ((idea)))) (equal?? (down '(a (more (complicated)) object)) '((a) ((more (complicated))) (object))) (report-unit-tests-completed 'down) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Problem 3: EOPL 1.20. ;; count-occurrences : symbol * s-list -> integer ;; count-sexp : symbol * s-exp -> integer ;; ;; (count-occurrences s slist) ;; returns the number of occurrences of s in slist. ;; ;; (count-sexp s sexp) ;; returns the number of occurrences of s in sexp. ;; ;; Recall from EOPL Definition 1.1.6 (page 8) that ;; s-list ::= a list of s-exp's ;; s-exp ::= Symbol | s-list (define count-occurrences (lambda (s slist) (if (null? slist) 0 (+ (count-sexp s (car slist)) (count-occurrences s (cdr slist)))))) (define count-sexp (lambda (s sexp) (if (symbol? sexp) (if (eqv? s sexp) 1 0) (count-occurrences s sexp)))) ;; tests (equal?? (count-sexp 'x 'x) 1) (equal?? (count-sexp 'x 'w) 0) (equal?? (count-occurrences 'x '()) 0) (equal?? (count-occurrences 'x '((f x) y (((x z) x)))) 3) (equal?? (count-occurrences 'x '((f x) y (((x z) () x)))) 3) (equal?? (count-occurrences 'w '((f x) y (((x z) x)))) 0) (report-unit-tests-completed 'count-occurrences)