;;; CSC 601: More Scheme practice. ;;; Examples from class, Sep 17. (define atom? (lambda (x) (not (pair? x)))) (define and~ (lambda flist (lambda (x) (if (null? flist) #t (and ((car flist) x) ((apply and~ (cdr flist)) x)))))) (define matches? ; Match a pattern (tree of predicates) to a tree of objects. (lambda (predtree objtree) (cond ((null? objtree) (null? predtree)) ((atom? objtree) ((eval predtree) objtree)) (#t (and (pair? predtree) (matches? (car predtree) (car objtree)) (matches? (cdr predtree) (cdr objtree))))))) (define s '(person (name "Tir") (age 25))) (define s2 '(person (name "Tir") (age 25) (id 3098098))) (define s3 '(person (name "Tir") (age 2.5))) (define person- '(symbol? (symbol? string?) (symbol? (and~ number? positive? integer?)))) (matches? person- s ) ;=> #t (matches? person- s2) ;=> #f (matches? person- s3) ;=> #f