Welcome to W3Courses
 Like Us on Facebook

Cartesian Product Using Scheme Source Code

Cartesian product code

CODE
 (define cart_prod (lambda (x y)
       (define prod (lambda (lis)
       (if (null? lis) '()
            (let ((elt (car lis)))
            (define map-d (lambda (L)
                  (if (null? L) '()
                  (cons (list elt (car L))
                        (map-d (cdr L))))))
            (append (map-d y) (prod (cdr lis)))))))
                  (prod x)))


RESULT
> (cart_prod '(a b c) '(d e f))
((a d) (a e) (a f) (b d) (b e) (b f) (c d) (c e) (c f))
> (cart_prod '(a b) '(c d))
((a c) (a d) (b c) (b d))

Share