;; 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) ;; Examples (SExp) 3 'red "yellow" empty (define sexp1 (list "banana" 27 'grapefruit)) (define sexp2 (list (list 'red 'yellow (list "banana" 27 'grapefruit)) (list 1 2) "foo")) ;; atom-occurs? : SExp Atom -> Boolean ;; does the atom occur anywhere in the given sexp? (define (atom-occurs? s a) (cond [(atom? s) (atom=? s a)] [else (los-atom-occurs? s a)])) ;; los-atom-occurs? : LOS Atom -> Boolean ;; does the atom occur in los? (define (los-atom-occurs? los a) (cond [(empty? los) false] [(cons? los) (or (atom-occurs? (first los) a) (los-atom-occurs? (rest los) a))])) ;; template for SExp #;(define (sexp-temp s) (cond [(atom? s) ...] [else ... (los-temp s) ...])) ;; template for LOS #;(define (los-temp los) (cond [(empty? los) ...] [(cons? los) ... (sexp-temp (first los)) ... (los-temp (rest los))])) (check-expect (atom-occurs? sexp2 "banana") true) (check-expect (atom-occurs? 5 "banana") false) (check-expect (atom-occurs? sexp1 'yellow) false) (check-expect (atom-occurs? empty "banana") false) #| ;; flatten.v1 : SExp -> SExp ;; flatten the given sexpr (see examples) (define (flatten.v1 s) (cond [(atom? s) s] [else (los-flatten.v1 s)])) ;; los-flatten.v1 : LOS -> LOS (define (los-flatten.v1 los) (cond [(empty? los) empty] [(cons? los) (append (if (atom? (first los)) (list (flatten.v1 (first los))) (flatten.v1 (first los))) (los-flatten.v1 (rest los)))])) #;(define (los-flatten.v1 los) (cond [(empty? los) empty] [(cons? los) (if (atom? (first los)) (append (list (flatten.v1 (first los))) (los-flatten.v1 (rest los))) (append (flatten.v1 (first los)) (los-flatten.v1 (rest los))))])) (check-expect (flatten.v1 4) 4) (check-expect (flatten.v1 "red") "red") (check-expect (flatten.v1 empty) empty) (check-expect (flatten.v1 sexp1) sexp1) (check-expect (flatten.v1 sexp2) (list 'red 'yellow "banana" 27 'grapefruit 1 2 "foo")) |# ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; flatten : SExp -> [List-of Atom] ;; flatten the given sexpr (see examples) (define (flatten s) (cond [(atom? s) (list s)] [else (los-flatten s)])) ;; los-flatten : LOS -> [List-of Atom] (define (los-flatten los) (foldr (lambda (x ans) (append (flatten x) ans)) empty los)) #;(cond [(empty? los) empty] [(cons? los) (append (flatten (first los)) (los-flatten (rest los)))]) (check-expect (flatten 4) (list 4)) (check-expect (flatten "red") (list "red")) (check-expect (flatten empty) empty) (check-expect (flatten sexp1) sexp1) (check-expect (flatten sexp2) (list 'red 'yellow "banana" 27 'grapefruit 1 2 "foo")) ;; Consider '(+ (* 5 4) 8) ;; This is an s-expression that represents the program (+ (* 5 4) 8) ;; which you may write in BSL/ISL. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; QUIZ and solution ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; AExp is one of ;; - 'x ;; - Number ;; - (make-unary AExp) ;; - (make-add AExp AExp) (define-struct unary (exp)) (define-struct add (left right)) ;; subst: AExp Number -> AExp ;; replace all occurrences of 'x in a with n (define (subst a n) (cond [(symbol? a) n] [(number? a) a] [(unary? a) (make-unary (subst (unary-exp a) n))] [(add? a) (make-add (subst (add-left a) n) (subst (add-right a) n))])) (check-expect (subst 'x 4) 4) (check-expect (subst 100 4) 100) (check-expect (subst (make-unary (make-unary 'x)) 4) (make-unary (make-unary 4))) (check-expect (subst (make-add (make-unary 'x) (make-unary 'x)) 4) (make-add (make-unary 4) (make-unary 4))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Trees ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (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 height, which given a Tree, returns its height ;; height : Tree -> Natural (non-negative integer) ;; given t, return its height (define (height t) (cond [(symbol? t) 0] [(branch? t) (add1 (height (branch-next t)))] [(fork? t) (add1 (max (height (fork-left t)) (height (fork-right t))))])) (define t1 'a) (define t2 (make-branch 'b t1)) (define t3 (make-fork 'c t1 t1)) (define t4 (make-fork 'c t1 t2)) (check-expect (height t1) 0) (check-expect (height t2) 1) (check-expect (height t3) 1) (check-expect (height t4) 2)