;; This procedure is equivalent to the two mutually recursive ;; procedures even-part and odd-part on page 184 from the book. (define 2-way-split (lambda (which lst) (cond ((null? lst) '()) ((equal? which 'odd) (cons (car lst) (2-way-split 'even (cdr lst)))) ((equal? which 'even) (2-way-split 'odd (cdr lst))) (else (error "wrong argument in 2-way-split" which))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; We can write mutually recursive procedures to split a list into ;; three equal (well, as equal as possible) parts, as follows. (define first-part (lambda (lst) (if (null? lst) '() (cons (car lst) (third-part (cdr lst)))))) (define third-part (lambda (lst) (if (null? lst) '() (second-part (cdr lst))))) (define second-part (lambda (lst) (if (null? lst) '() (first-part (cdr lst))))) ;; Or equivalently as one procedure. (define 3-way-split (lambda (which lst) (cond ((null? lst) '()) ((equal? which 'first) (cons (car lst) (3-way-split 'third (cdr lst)))) ((equal? which 'second) (3-way-split 'first (cdr lst))) ((equal? which 'third) (3-way-split 'second (cdr lst))) (else (error "wrong argument in 3-way-split" which)))))