fork download
  1. ; common elements of three arrays
  2.  
  3. (define (fold-left op base xs)
  4. (if (null? xs)
  5. base
  6. (fold-left op (op base (car xs)) (cdr xs))))
  7.  
  8. (define (common2 xs ys)
  9. (let loop ((xs xs) (ys ys) (zs (list)))
  10. (cond ((or (null? xs) (null? ys))
  11. (reverse zs))
  12. ((< (car xs) (car ys))
  13. (loop (cdr xs) ys zs))
  14. ((< (car ys) (car xs))
  15. (loop xs (cdr ys) zs))
  16. (else (loop (cdr xs) (cdr ys)
  17. (cons (car xs) zs))))))
  18.  
  19. (define (common3 xs ys zs)
  20. (common2 (common2 xs ys) zs))
  21.  
  22. (define xs '(1 5 10 20 40 80))
  23. (define ys '(6 7 10 20 80 100))
  24. (define zs '(3 4 15 20 30 70 80 120))
  25.  
  26. (display (common2 xs ys)) (newline)
  27. (display (common2 ys zs)) (newline)
  28. (display (common2 xs zs)) (newline)
  29. (display (common2 (common2 xs ys) zs)) (newline)
  30. (display (common3 xs ys zs)) (newline)
  31. (display (common3 '(1 5 5 5) '(3 4 5 5 10) '(5 5 10 20))) (newline)
  32.  
  33. (define (common . xss)
  34. (fold-left common2 (car xss) (cdr xss)))
  35.  
  36. (display (common xs ys zs)) (newline)
Success #stdin #stdout 0s 7272KB
stdin
Standard input is empty
stdout
(10 20 80)
(20 80)
(20 80)
(20 80)
(20 80)
(5 5)
(20 80)