Cartesian product of two or more lists: Difference between revisions

Line 886:
Racket has a built-in "cartesian-product" function:
 
<lang>#lang racket/base
(require rackunit
</lang>
;; usually, included in "racket", but we're using racket/base so we
;; show where this comes from
(only-in racket/list cartesian-product))
;; these tests will pass silently
(check-equal? (cartesian-product '(1 2) '(3 4))
'((1 3) (1 4) (2 3) (2 4)))
(check-equal? (cartesian-product '(3 4) '(1 2))
'((3 1) (3 2) (4 1) (4 2)))
(check-equal? (cartesian-product '(1 2) '()) '())
(check-equal? (cartesian-product '() '(1 2)) '())
 
;; these will print
(cartesian-product '(1776 1789) '(7 12) '(4 14 23) '(0 1))
(cartesian-product '(1 2 3) '(30) '(500 100))
(cartesian-product '(1 2 3) '() '(500 100))</lang>
 
{{out}}
 
<pre>'((1776 7 4 0)
(1776 7 4 1)
</pre>
(1776 7 14 0)
(1776 7 14 1)
(1776 7 23 0)
(1776 7 23 1)
(1776 12 4 0)
(1776 12 4 1)
(1776 12 14 0)
(1776 12 14 1)
(1776 12 23 0)
(1776 12 23 1)
(1789 7 4 0)
(1789 7 4 1)
(1789 7 14 0)
(1789 7 14 1)
(1789 7 23 0)
(1789 7 23 1)
(1789 12 4 0)
(1789 12 4 1)
(1789 12 14 0)
(1789 12 14 1)
(1789 12 23 0)
(1789 12 23 1))
'((1 30 500) (1 30 100) (2 30 500) (2 30 100) (3 30 500) (3 30 100))
'()</pre>
 
=={{header|Ruby}}==
569

edits