Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
The cartesian product of two sets


Here follows two slightly different solutions. The first without reduce.

(define (cartesian-product set-list-1 set-list-2) (apply append (map (lambda (b) (map (lambda (a) (cons a b)) set-list-1)) set-list-2))) (define (cartesian-product set-list-1 set-list-2) (reduce-right append (map (lambda (b) (map (lambda (a) (cons a b)) set-list-1)) set-list-2))) (define (reduce-right f lst) (if (null? (cdr lst)) (car lst) (f (car lst) (reduce-right f (cdr lst)))))

The first solution comes from Christian Wagenknecht's book 'Programmierparadigmen', from Springer Verlag.