[racket] sicp exercise 2.20

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Jul 20 09:54:01 EDT 2010


Do you mean this?

#lang racket

(require rackunit)

;; Nat Nat *-> [Listof Nat]
;; which of the
(define (same-parity x . xs)
   (cons x (filter (λ (o) (or (and (even? x) (even? o)) (and (odd? x)  
(odd? o)))) xs)))

(check-equal? (same-parity 1 2 3 4 5 6 7) '(1 3 5 7))
(check-equal? (same-parity 2 3 4 5 6 7) '(2 4 6))




On Jul 20, 2010, at 9:36 AM, Martin DeMello wrote:

> By way of a puzzle, I've been trying to solve SICP exercise 2.20
> ----------------------------
> Using the (define (f x . args)) notation, 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)
> ----------------------------
> using "straight recursion", that is, without using any `let` or
> `define` constructs. Still not managed to find the trick that will
> tack on the first argument as the head of the list. Anyone have a
> hint?
>
> martin
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users



Posted on the users mailing list.