; A good idea ... write (define slist? ...) (define (contains? slist sym) (let in-list? ([slist slist]) (cond [(null? slist) #f] [(symbol? (car slist)) (or (eq? (car slist) sym) (in-list? (cdr slist)))] [else ; car is an slist (or (in-list? (car slist)) (in-list? (cdr slist)))] ))) (contains? '() 'a) ; #f (contains? '(b a) 'a) ; #t (contains? '(( b (a)) ()) 'a) ; #t (contains? '((c b ()) ( b (c a)) ()) 'a) ; #t (contains? '((c b ()) ( b (c a)) ()) 'p) ; #f (define (count-occurrences slist sym) (let count ([slist slist]) (cond [(null? slist) 0] [(symbol? (car slist)) (+ (if (eq? (car slist) sym) 1 0) (count (cdr slist)))] [else ; car is an slist (+ (count (car slist)) (count (cdr slist)))] ))) (count-occurrences '() 'a) ; 0 (count-occurrences '(b a b () a b) 'a) ; 2 (count-occurrences '(b a b () a b) 'b) ; 3 (count-occurrences '(b ((a) a) b () a b) 'a) ; 3 (count-occurrences '((b ((a) a) b () a b)) 'a) ; 3 (define (notate-depth slist) (let notate ([slist slist][depth 1]) (cond [(null? slist) '()] [(symbol? (car slist)) (cons (list (car slist) depth) (notate (cdr slist) depth))] [else ; car is an slist (cons (notate (car slist) (+ 1 depth)) (notate (cdr slist) depth))] ))) (notate-depth '()) ; () (notate-depth '(a)) ; ((a 1)) (notate-depth '((a b) c)) ; (((a 1) (b 2)) (c 1)) (notate-depth '( () (a (b)) c ((d) () e))) ; (() ((a 2) ((b 3))) (c 1) (((d 3)) () (e 2))) (notate-depth '((() (a (b)) c ((d) () e)))) ; ((() ((a 3) ((b 4))) (c 2) (((d 4)) () (e 3)))) (define (flatten slist) (trace-let flatten ([slist slist]) (cond [(null? slist) '()] [(symbol? (car slist)) (cons (car slist) (flatten (cdr slist)))] [else ; car is an slist (append (flatten (car slist)) (flatten (cdr slist)))] ))) (flatten '( () (a ((b) c () ((d e ((f))) g) h)) ())) ; (a b c d e f g h) (define (subst s1 s2 slist) (subst 'a 'b '(() a (c ((a) a) (c (((c a))))))) ; (() b (c ((b) b) (c (((c b)))))