fork download
  1. (define (same-parity a . xs)
  2. (let ((parity (if (odd? a) odd? even?)))
  3. (define (iter items result)
  4. (if (null? items)
  5. result
  6. (iter (cdr items)
  7. (if (parity (car items))
  8. (cons result (car items))
  9. result))))
  10. (iter xs a)))
  11.  
  12. ; Testing
  13.  
  14. (display (same-parity 1 2 3 4 5 6 7 8 9 10))
  15. (newline)
  16.  
Success #stdin #stdout 0.03s 8656KB
stdin
Standard input is empty
stdout
((((1 . 3) . 5) . 7) . 9)