Blob


1 ;; 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,
3 ;; (define (same-parity parity . integers)
4 ;; (let ((rem (remainder parity 2)))
5 ;; (define (par ints)
6 ;; (cond ((null? ints) '())
7 ;; ((= (remainder (car ints) 2) rem)
8 ;; (cons (car ints)
9 ;; (par (cdr ints))))
10 ;; (else (par (cdr ints))))))
11 ;; (cons parity (par integers)))
13 ;; par seems not to be in scope in the call at the end
16 (define (same-parity parity . integers)
17 (let ((rem (remainder parity 2)))
18 (define (par ints)
19 (cond ((null? ints) '())
20 ((= (remainder (car ints) 2) rem)
21 (cons (car ints)
22 (par (cdr ints))))
23 (else (par (cdr ints)))))
24 (cons parity (par integers))))
27 (define (test-case actual expected)
28 (load-option 'format)
29 (newline)
30 (format #t "Actual: ~A Expected: ~A" actual expected))
32 (test-case (same-parity 1 2 3 4 5 6 7) '(1 3 5 7))
33 (test-case (same-parity 2 3 4 5 6 7) '(2 4 6))