;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname today-sexp) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; An Atom is one of: ;; - Symbol ;; - Number ;; - String #; ;; Atom -> ? (define (atom-temp a) (cond [(symbol? a) ...] [(number? a) ...] [(string? a) ...])) ;; atom=? : Atom Atom -> Boolean ;; Are the two atoms the same? (check-expect (atom=? "Bob" "Bob") true) (check-expect (atom=? 'Bob 'Alice) false) (check-expect (atom=? "Bob" 'Bob) false) (check-expect (atom=? 7 7) true) (check-expect (atom=? 7 "seven") false) (define (atom=? a b) (cond [(symbol? a) (and (symbol? b) (symbol=? a b))] [(number? a) (and (number? b) (= a b))] [(string? a) (and (string? b) (string=? a b))])) ;; atom? : Any -> Boolean ;; Is this thing an atom? (check-expect (atom? "Bob") true) (check-expect (atom? (cons 1 empty)) false) (define (atom? x) (or (symbol? x) (number? x) (string? x))) ;; An SExp is one of: ;; - empty ;; - (cons Atom SExp) ;; - (cons SExp SExp) #; ;; SExp -> ? (define (sexp-temp s) (cond [(empty? s) ...] [(atom? (first s)) (atom-temp (first s)) ... (sexp-temp (rest s))] [else (sexp-temp (first s)) (sexp-temp (rest s))])) ;; a-occurs? : SExp -> Boolean ;; Does 'a occur in the s-expression? (check-expect (a-occurs? (list 'a)) true) (check-expect (a-occurs? (list (list (list (list (list (list (list (list (list 'a)))))))))) true) (check-expect (a-occurs? (list 'b 'c (list 'a))) true) (check-expect (a-occurs? empty) false) (check-expect (a-occurs? (list 'b 'c (list 4))) false) (define (a-occurs? s) (occurs? s 'a)) ;; b-occurs? : SExp -> Boolean ;; Does 'b occur in the s-expression? (check-expect (b-occurs? (list 'b)) true) (check-expect (b-occurs? (list (list (list (list (list (list (list (list (list 'b)))))))))) true) (check-expect (b-occurs? (list 'b 'c (list 'b))) true) (check-expect (b-occurs? empty) false) (check-expect (b-occurs? (list 'a 'c (list 4))) false) (define (b-occurs? s) (occurs? s 'b)) ;; occurs? : SExp Atom -> Boolean ;; Does the atom occur in the s-expression? (define (occurs? s a) (cond [(empty? s) false] [(atom? (first s)) (or (atom=? (first s) a) (occurs? (rest s) a))] [else (or (occurs? (first s) a) (occurs? (rest s) a))]))