<< Previous exercise (2.19) | Index | Next exercise (2.21) >>

 (define (same-parity first . rest) 
   (define (same-parity-iter source dist remainder-val) 
     (if (null? source) 
         (same-parity-iter (cdr source) 
                           (if (= (remainder (car source) 2) remainder-val) 
                               (append dist (list (car source))) 
   (same-parity-iter rest (list first) (remainder first 2))) 


Using append as above is expensive, as it will iterate through all the elements of the list you build, at _each_ iteration.

It's better simply to build the list in reverse order, and then reverse the final list at the end

 (define (same-parity first . rest) 
   (let ((yes? (if (even? first) 
     (define (iter items result) 
       (if (null? items) 
           (reverse result) 
           (iter (cdr items) (if (yes? (car items)) 
                                 (cons (car items) result) 
     (iter rest (list first)))) 

 (define (sam-parity first . rest) 
   (define (inter yes? lat) 
       ((null? lat) (quote())) 
       ((yes? (car lat))(cons (car lat) (inter yes? (cdr lat)))) 
        (inter yes? (cdr lat))))) 
   (if (odd? first) 
       (inter odd? rest) 
       (inter even? rest)))  


 ;; ex 2.20, dotted-tail notation. 
 (define (same-parity first . rest) 
   (define (congruent-to-first-mod-2? a) 
     (= (remainder a 2) (remainder first 2))) 
   (define (select-same-parity items) 
     (if (null? items)  
         (let ((curr (car items)) 
               (select-rest (select-same-parity (cdr items)))) 
           (if (congruent-to-first-mod-2? curr) 
               (cons curr select-rest) 
   (cons first (select-same-parity rest))) 
 ;; an alternative implementation by andras: 
 (define (same-parity a . l) 
         (define (sp-builder result tail) 
         (if (null? tail) 
         (if (even? (+ a (car tail))) 
         ;;test for same parity 
         ;;if the current beginning of the rest (car tail) is the same parity as "a", then it is appended to the result, else the result is left untouched 
         (sp-builder (append result (list (car tail))) (cdr tail)) 
         (sp-builder result (cdr tail))))) 
         (sp-builder (list a) l)) 
 ;; Usage: 
 (same-parity 1 2 3 4 5 6 7) 
 ;; (1 3 5 7) 
 (same-parity 2 3 4 5 6 7 8) 
 ;; (2 4 6 8) 


This does it by passing the relevant test:

 (define (same-parity . l) 
   (define (parity l test) 
     (if (null? l) 
         (if (test (car l)) 
             (cons (car l)(parity (cdr l) test)) 
             (parity (cdr l) test)))) 
   (if (even? (car l)) 
       (parity l even?) 
       (parity l odd?))) 


     (define (same-parity x . y) 
         ;; Finds the list of elements in y with same parity as x 
         (define (same-parity-rest y)   
                     (null? y) '() 
                 (parity-2 (car y)  x) 
                     (cons (car y) (same-parity-rest (cdr y)) )   
                     else            ;;(car y)  has  different parity than  x.. 
                     (same-parity-rest (cdr y)) 
         ;; Check if 2 integers have same parity 
         (define (parity-2 a b) 
             (= (remainder (+ a b) 2) 0)  ;; is thier sum even ? 
         ;; Combine the result 
         (cons x (same-parity-rest y)) 


 (define (same-parity n . lst) 
    (define same-parity? (if (even? n) even? odd?)) 
    (define (iter lst acc) 
      (if (null? lst) 
          (let ((first (car lst)) 
                (rest (cdr lst))) 
            (iter rest 
                  (if (same-parity? first) 
                      (cons first acc) 
    (cons n (reverse (iter lst null)))) 

The footnote for this section shows a funny lambda:

  (define f (lambda (x y . z) z))
  (f 1 2 3 4 5)
  => (3 4 5)

Defining a lambda that takes a variable number of args is isn't done in the obvious way:

  (define h (lambda (. w) w))
  ;Ill-formed dotted list: (. w) ... etc, lots of errors.

This has to be defined like this:

  (define h (lambda w w))
  (h 1 2 3 4)
  => (1 2 3 4)


 (define (same-parity x . y) 
   (define (parity list rem) 
     (cond ((null? list) list) 
           ((= rem (remainder (car list) 2)) 
            (cons (car list) (parity (cdr list) rem) )) 
            (parity (cdr list) rem)))) 
   (if (even? x) 
       (parity y 0) 
       (parity y 1))) 


Recursive process

 (define (same-parity . L) 
   (define (filter x) 
     (if (even? x) 
   (define (construct f L) 
     (cond ((null? L) '()) 
           ((f (car L)) (cons (car L)  
                                     (construct f (cdr L)))) 
           (else (construct f (cdr L))))) 
   (construct (filter (car L)) L)) 
 (same-parity 2 3 4 5 6 7) ;; '(2 4 6) 


My solution.

  (define (same-parity x . w) 
     (define (get-all w r) 
       (if (null? w) `() 
           (if (= (remainder (car w) 2) r) (cons (car w) (get-all (cdr w) r)) 
               (get-all (cdr w) r)))) 
     (cons x (get-all w (remainder x 2)))) 

It is possible to test for parity using just the sum of two terms (odd+odd=pair pair+pair=pair). Bellow Using the sum of first and each new term.

 (define (same-parity first . l)    
     (define (iter lista lfinal) 
         (if (null? lista)  
             (if (even? (+ first (car lista))) 
                 (iter (cdr lista) (append lfinal (list (car lista)))) 
                 (iter (cdr lista) lfinal)))) 
     (iter l (list first))) 


My recursive solution.

 (define (same-parity x . y) 
   (define (search-parity a r) 
     (if (null? r) 
       (if (= (remainder a 2) (remainder (car r) 2)) 
           (cons (car r) (search-parity a (cdr r))) 
         (search-parity a (cdr r))))) 
   (cons x (search-parity x y))) 


 (define (find-numbers items condition) 
   (if (null? items) 
       (if (condition (car items)) 
           (cons (car items) (find-numbers (cdr items) condition)) 
           (find-numbers (cdr items) condition)))) 
 (define (same-parity first . items) 
   (if (odd? first) 
       (find-numbers items odd?) 
       (find-numbers items even?))) 

Here is my solution

 (define (same-parity first . items) 
   (define (iter items) 
     (if (null? items) 
     (if (even? (+ (car items) first)) 
         (cons (car items) (iter (cdr items))) 
         (iter (cdr items))))) 
   (cons first (iter items))) 



solution using filter

 (define (same-parity . l) 
   (define (first-parity) 
     (if (even? (car l)) 
   (filter (first-parity) l)) 


Storing the function to apply (even? or odd?) as predicate and applying it using filter

 (define (same-parity fst . rest) 
   (let ((pred (if (even? fst) even? odd?))) 
     (filter pred (cons fst rest)))) 


My solution using recursion

 (define (same-parity i . l) 
   (define (same-parity-i x) 
     (if (even? (+ i x)) #t #f)) 
   (cond ((null? l) i) 
         ((same-parity-i (car l)) 
          (cons i (apply same-parity l))) 
         (else (apply same-parity i (cdr l))))) 


This solution leverages abstraction, by introducing a `sub-list` procedure that returns a subset of the input that matches a predicate. `same-parity` is defined in terms of `sub-list`.

 (define (sub-list predicate? x) 
   (define (sub-list-iter items result) 
     (if (null? items) (reverse result) 
         (let ((caritems (car items))) 
             ((predicate? caritems) 
              (sub-list-iter (cdr items) 
                             (cons caritems result))) 
               (sub-list-iter (cdr items) result)))))) 
   (sub-list-iter x nil)) 
 (define (same-parity x . A) 
   (if (even? x)  
       (sub-list even? A) 
       (sub-list odd? A))) 


Works but isn't too pretty

 (define (same-parity x . y) 
   (define (filter p items) 
     (cond ((null? items) #nil) 
           ((p (car items)) (cons (car items) (filter p (cdr items)))) 
           (else (filter p (cdr items))))) 
   (let ((parity (remainder x 2))) 
     (cons x (filter (lambda (x) (= parity (remainder x 2))) y)))) 

It's surprising how many of the above solutions use functions that have not been introduced in the book at this point. Here's my solution:

 (define (same-parity . args) 
         (define (same-parity-step parity lst) 
                 (cond ((null? lst) NIL) 
                       ((= parity (remainder (car lst) 2)) (cons (car lst) (same-parity-step parity (cdr lst)))) 
                       (else (same-parity-step parity (cdr lst))) 
         (same-parity-step (remainder (car args) 2) args) 


The way I did it.

 (define (same-parity a . b) 
   (define (iter b result) 
     (cond ((null? b) 
            (reverse result)) 
           ((or (and (even? a) (even? (car b))) 
                (and (odd? a) (odd? (car b)))) 
            (iter (cdr b) (cons (car b) result))) 
            (iter (cdr b) result)))) 
   (iter b (list a))) 

Or non-iteratively.

 (define (same-parity a . b) 
   (define (aux b) 
     (cond ((null? b) 
           ((or (and (even? a) (even? (car b))) 
                (and (odd? a) (odd? (car b)))) 
            (cons (car b) (aux (cdr b)))) 
            (aux (cdr b))))) 
   (cons a (aux b)))