;; Processing TWO complex inputs ;; An Atom is one of: ;; - Number ;; - Symbol ;; - String ;; atom? : Anything -> Boolean ;; is the input a atom? (define (atom? a) (or (number? a) (symbol? a) (string? a))) ;; atom=? : Atom Atom -> Boolean ;; are the two atoms equal? (define (atom=? a1 a2) (cond [(number? a1) (and (number? a2) (= a1 a2))] [(symbol? a1) (and (symbol? a2) (symbol=? a1 a2))] [(string? a1) (and (string? a2) (string=? a1 a2))])) (check-expect (atom=? 3 'foo) false) (check-expect (atom=? 3 3) true) ;;;;;;;;;;;;;;;;;;;; ;; An SExp is one of: ;; - Atom ;; - LOS ;; An LOS (listof SExp) is one of: ;; - empty ;; - (cons SExp LOS) ;; list? : Anything -> Boolean ;; is x a list? (define (list? x) (or (empty? x) (cons? x))) ;; DESIGN sexp=? ;; sexp=? : SExp SExp -> Boolean ;; Is s1 equal to s2? (define (sexp=? s1 s2) (cond [(and (atom? s1) (atom? s2)) (atom=? s1 s2)] [(and (atom? s1) (list? s2)) false] [(and (list? s1) (atom? s2)) false] [(and (list? s1) (list? s2)) (los=? s1 s2)])) ;; los=? : LOS LOS -> Boolean ;; is los1 equal to los2? (define (los=? los1 los2) (cond [(and (empty? los1) (empty? los2)) true] [(and (empty? los1) (cons? los2)) false] [(and (cons? los1) (empty? los2)) false] [(and (cons? los1) (cons? los2)) (and (sexp=? (first los1) (first los2)) (los=? (rest los1) (rest los2)))])) ;; Examples (SExp) 3 'red "yellow" empty (define s1 (list "banana" 27 'grapefruit)) (define s2 (list (list (list "banana" 27 'grapefruit) 'red 'yellow) (list 1 2) "foo")) (check-expect (sexp=? 3 "yellow") false) (check-expect (sexp=? 3 3) true) (check-expect (sexp=? empty "yellow") false) (check-expect (sexp=? empty empty) true) (check-expect (sexp=? "yellow" s1) false) (check-expect (sexp=? s2 s2) true) (check-expect (sexp=? s1 (first (first s2))) true) ;-------------------------------------------------------- (define-struct branch (value next)) (define-struct fork (value left right)) ;; A Tree is one of: ;; - Symbol ;; - (make-branch Symbol Tree) ;; - (make-fork Symbol Tree Tree) ;; DESIGN tree=? ;; tree=? : Tree Tree -> Boolean ;; is t1 equal to t2? (define (tree=? t1 t2) (cond [(and (symbol? t1) (symbol? t2)) (symbol=? t1 t2)] [(and (symbol? t1) (branch? t2)) false] [(and (symbol? t1) (fork? t2)) false] [(and (branch? t1) (symbol? t2)) false] [(and (branch? t1) (branch? t2)) (and (symbol=? (branch-value t1) (branch-value t2)) (tree=? (branch-next t1) (branch-next t2)))] [(and (branch? t1) (fork? t2)) false] [(and (fork? t1) (symbol? t2)) false] [(and (fork? t1) (branch? t2)) false] [(and (fork? t1) (fork? t2)) (and (symbol=? (fork-value t1) (fork-value t2)) (tree=? (fork-left t1) (fork-left t2)) (tree=? (fork-right t1) (fork-right t2)))])) (define t1 'a) (define t2 (make-branch 'b t1)) (define t3 (make-fork 'c t1 t1)) (define t4 (make-fork 'c t1 t2)) (define t5 (make-branch 'b 'a)) (check-expect (tree=? t1 t1) true) (check-expect (tree=? t1 'b) false) (check-expect (tree=? t1 t2) false) (check-expect (tree=? t2 t5) true) (check-expect (tree=? t4 t3) false) (check-expect (tree=? t4 t4) true) ;-------------------------------------------------------- ;; DESIGN zip which takes two lists and zips them together ;; Examples: ;; (zip '(1 3 5 7) '(2 4 6 8)) --> '(1 2 3 4 5 6 7 8) ;; (zip '(1 3) '(2 4 6 8)) --> '(1 2 3 4 6 8) ;; (zip '(1 3 5 7) '(2) --> '(1 2 3 5 7)