; Deep recursion: recursion on lists, where we recurse into the nested lists ; in practice, you have one more case to deal with, the case when (car ls) ; is a list. In that case you will want to recurse on the car and the cdr ; example, deep-element?, which takes an item A and a list LS, and returns ; #t if A lies in any of the nested lists of LS ; ex: (deep-element? 3 '((1 ((2) 3) 4) 5)) --> #t (define deep-element? (lambda (a ls) ; good to start with cond for deep recursion (cond ((null? ls) #f) ((list? (car ls)) (or (deep-element? a (car ls)) (deep-element? a (cdr ls)))) ((equal? a (car ls)) #t) (else (deep-element? a (cdr ls)))))) ; you want to use deep recursion for practice in the lab, but if in the future you don't ; want to use it, you can get around it with this procedure... ; FLATTEN: takes in a nested list and flattens it so it has all the items of the nested list ; but no inner lists ; ex: (flatten '((1 ((2) 3) 4) 5)) --> '(1 2 3 4 5) ; note this will not help you with DEPTH, or related procedures (define flatten (lambda (ls) (cond ((null? ls) '()) ((list? (car ls)) (append (flatten (car ls)) (flatten (cdr ls)))) (else (cons (car ls) (flatten (cdr ls))))))) ; COUNT-THIS-SYMBOL: will return the number of occurences of the ; given symbol in the given list (define count-this-symbol (lambda (ls sym) (cond ((null? ls) 0) ((equal? (car ls) sym) (+ 1 (count-this-symbol (cdr ls) sym))) ((list? (car ls)) (+ (count-this-symbol (car ls) sym) (count-this-symbol (cdr ls) sym))) (else (count-this-symbol (cdr ls) sym))))) (count-this-symbol '(a (b (c a) a) a) 'a) ; returns 4