;; 5.1 (define remove-all (lambda (l) (cond ((null? l) '()) ((pair? (car l)) (cons (remove-all (car l)) (remove-all (cdr l)))) (else (remove-all (cdr l)))) )) ;; 5.2 ; append is predefined, but here it is for reference. (define append-lists (lambda (l1 l2) (cond ((null? l1) l2) (cons (car l1) (append-lists (cdr l1) l2)) ))) ; snoc: put an item on the end of a list. ; used as a helper for reverse-list. (define snoc (lambda (l x) (cond ((null? l) (list x)) (else (cons (car l) (snoc (cdr l) x)))))) (define reverse-list (lambda (l) (cond ((null? l) '()) ((pair? (car l)) (snoc (reverse-list (cdr l)) (reverse-list (car l)))) (else (snoc (reverse-list (cdr l)) (car l)))))) ;; 5.3 ;; make-single written with an accumulator to hold the ;; previous value for comparison. (null is used for the empty value) (define make-single-acc (lambda (l prev) (cond ((null? l) '()) ((pair? (car l)) (cons (make-single-acc (car l) '()) (make-single-acc (cdr l) '()))) (else (if (null? prev) (cons (car l) (make-single-acc (cdr l) (car l))) (if (eq? (car l) prev) (make-single-acc (cdr l) prev) (cons (car l) (make-single-acc (cdr l) (car l))))))))) (define make-single (lambda (l) (make-single-acc l '())))