;;; The CS2500 Guide to Better SExps. ;;; An Atom is one of: ;;; - Number ;;; - String ;;; - Symbol ;;; Is the argument an atom? ;;; Anything -> Boolean (define (atom? x) (or (number? x) (string? x) (symbol? x))) ;;; Are the two atoms identical? ;;; Atom Atom -> Boolean (define (atom=? x y) (cond [(number? x) (and (number? y) (= x y))] [(string? x) (and (string? y) (string=? x y))] [else (and (symbol? y) (symbol=? x y))])) ;;; A SExp (s-expression) is one of: ;;; - Atom ;;; - [Listof SExp] ;;; ;;; A [Listof SExp] is one of: ;;; - empty ;;; - (cons SExp [Listof SExp]) ;;; Mutually recursive! ;;; What are some examples of SExps? ;;; How does this space of values relate to the space of things ;;; we can write with ' notation? ;;; SExp Atom -> Boolean ;;; Does the atom occur anywhere in the SExp? (define (sexp-contains? s a) (cond [(atom? s) (atom=? s a)] [else (ormap (lambda (sexp) (sexp-contains? sexp a)) s)])) ;;; Two cross-product templates: ;;; SExp SExp -> Boolean ;;; Are the two sexps equal? (define (ekwal? a b) (cond [(and (atom? a) (atom? b)) (atom= a b)] [(and (atom? a) (not (atom? b))) false] [(and (not (atom? a)) (atom? b)) false] [else (ekwal-lists? a b)])) ;;; [Listof SExp] [Listof SExp] -> Boolean ;;; Are the two sexp-lists equal? (define (ekwal-lists? as bs) (cond [(and (empty? as) (empty? bs)) true ] [(and (empty? as) (not (empty? bs))) false] [(and (not (empty? as)) (empty? bs)) false] [else (and (ekwal? (first as) (first bs)) (ekwal-lists? (rest as) (rest bs)))]))