;;; Admin: ;;; - lab seriousness ;;; - screwing up templates / rote memorisation ;;; - what is grad school? (define title "S-expressions, Trees and Forests") (define description "") ;; display the below code and ask what it does ;; open browser to design recipe page ;;============================================================ ;; ;; An ATOM is one of: ;; -- Symbol ;; -- String ;; -- Number ;; Anything -> Boolean ;; Is x an atom? (define (atom? x) (or (symbol? x) (string? x) (number? x))) ;; Atom Atom -> Boolean ;; Is x the same atom as y? (define (atom=? x y) (cond [(and (symbol? x) (symbol? y)) (symbol=? x y)] [(and (string? x) (string? y)) (string=? x y)] [(and (number? x) (number? y)) (= x y)] [else false])) 'testing-atom? (equal? (atom? 'x) true) (equal? (atom? 10) true) 'testing-atom=? (equal? (atom=? 'x 'y) false) (equal? (atom=? 10 10) true) (equal? (atom=? 'x "") false) (equal? (atom=? "" "a") false) ;; --------------------------------------------------------- ;; An SEXP (S-expression) is one of: ;; -- empty ;; -- (cons Atom SEXP) ;; -- (cons SEXP SEXP) ;; Have them create an instance or two. ;; Use 'a in the following examples, some in, some out. ;; Ask: Is (list (list 'a (list 'a)) 'c) ;; an SEXP? ;; Is (list true false) ;; an SEXP? ;; PROBLEM: ;; Design a function that checks whether 'a occurs ;; in a given SEXP. ;; occurs? : SEXP -> Boolean ;; Does 'a occur in s? ;; TEMPLATE: ask the questions (see web site) #; (define (occurs? s) (cond [(empty? s) ...] [(atom? (first s)) ... (first s) ... (occurs? (rest s)) ....] [else ... (occurs? (first s)) ... (occurs? (rest s))])) (define (occurs? s) (cond [(empty? s) false] [(atom? (first s)) (or (atom=? (first s) 'a) (occurs? (rest s)))] [else (or (occurs? (first s)) (occurs? (rest s)))])) 'testing-occurs? (equal? (occurs? (list 'a (list 'a))) true) (equal? (occurs? (list 'b (list 'a))) true) (equal? (occurs? (list "" (list "a"))) false) ;; Develop the next two on blackboard as well as computer. ;; --------------------------------------------------------- (define-struct binary (left right)) ;; A BT is one of ;; -- Atom ;; -- (make-binary BT BT) ;; Are 10 'a "hello world" ;; BTs? ;; Is (make-binary 'a (make-binary 10 20)) ;; a BT? ;; Is (make-binary 'a (make-binary 10 empty)) ;; a BT? ;; PROBLEM: ;; Design a function that checks whether 'a occurs ;; in a given BT. ;; bt-occurs? : BT -> Boolean ;; Does 'a occur in t? ;; TEMPLATE: ask the questions (see web site) #; (define (bt-occurs? t) (cond [(atom? t) ...] [(binary? t) ... (binary-occurs? (binary-left t)) ... ... (binary-occurs? (binary-right t)) ...])) (define (bt-occurs? t) (cond [(atom? t) (atom=? t 'a)] [(binary? t) (or (bt-occurs? (binary-left t)) (bt-occurs? (binary-right t)))])) 'testing-bt-occurs? (equal? (bt-occurs? 'a) true) (equal? (bt-occurs? (make-binary 1 2)) false) (equal? (bt-occurs? (make-binary 1 'a)) true) ;; --------------------------------------------------------- ;; A Forest is one of: ;; -- empty ;; -- (cons BT Forest) ;; Show connections among data definitions on blackboard. ;; Is empty ;; a Forest? ;; Are (list 10) (list 20 20) (list 'a 10 "hello") ;; Forests? ;; How many trees do they contain? ;; Is (list (make-binary 'a 'a) "hello" (make-binary 10 "world")) ;; a forest? ;; PROBLEM: ;; Design a function that checks whether 'a occurs ;; in a given Forest. ;; forest-occurs? : Forest -> Boolean ;; Does 'a occur in ft? ;; TEMPLATE #; (define (forest-occurs? ft) (cond [(empty? ft) ...] [else ... (bt-occurs? (first ft)) ... ... (forst-occurs? ft) ...])) ;; Show connections among templates on blackboard. (define (forest-occurs? ft) (cond [(empty? ft) false] [else (or (bt-occurs? (first ft)) (forest-occurs? (rest ft)))])) ;; Use check syntax to show connections among fun definitions 'testing-forest-occurs? (equal? (forest-occurs? empty) false) (equal? (forest-occurs? (list 'b (make-binary 'a 10) "hell")) true)