# weima learns to program

my attempt to do the exercises in sicp.

## Wednesday, July 16, 2008

### sicp exercise 2.20

; Exercise 2.20
;  Given the definition
;   (define (f x y . z) <body>)
;  the procedure f can be called with two or more arguments.
;  Use this notation to write a procedure same-parity that takes one or more integers and
;  returns a list of all the arguments that have the same even-odd parity as the
;  first argument. For example,
;      (same-parity 1 2 3 4 5 6 7)
;      (1 3 5 7)
;      (same-parity 2 3 4 5 6 7)
;      (2 4 6)

(define NULL (list))

(define same-parity-impl (lambda (parity items)
(if (null? items)
NULL
(if (parity (car items))
(cons (car items) (same-parity-impl parity (cdr items)))
(same-parity-impl parity (cdr items))))))

(define same-parity (lambda (parity . items)
(if (even? parity) (same-parity-impl even? (cons parity items))
(same-parity-impl odd? (cons parity items)))))

;(display (same-parity-impl even? (list 2 30 4 5))) (newline)
(display (same-parity  1 2 3 4 5 6 7 )) (newline)
(display (same-parity  2 3 4 5 6 7 8 )) (newline)