SICP: Structure and Interpretation of Computer Programs (24)

18 Name: #!/usr/bin/anonymous : 2008-06-19 01:25 ID:Heaven

>>17
Here's the exercise in common lisp

(defun ex2.20 (a &rest b)
(cons a (remove-if-not ((lambda (x) (if (oddp x) #'oddp #'evenp)) a) b)))

&rest is equal to '.'
scheme doesn't have remove-if-not so you can try to implement that

You must not return '() if other-values is an empty list. You must return the results or a.

For example,

(same-parity 1) ==> (1)
(same-parity 1 2 3) ==> (1 3)

Don't know how to use spoilers but here is same-parity in scheme:
.
.
.
.
.
.
.
.
.
.

(define (same-parity a . b)
(define (same-parity-help f a b)
(if (null? b) a
(same-parity-help f (if (f (car b)) (cons (car b) a) a) (cdr b))))
(cons a (reverse (same-parity-help (if (odd? a) odd? even?) '() b))))
This thread has been closed. You cannot post in this thread any longer.