Cartesian product of two or more lists: Difference between revisions
Content added Content deleted
(Added scheme) |
m (\ replaced with lambdas) |
||
Line 3,483: | Line 3,483: | ||
(if (or (zero? (length xs)) (zero? (length ys))) |
(if (or (zero? (length xs)) (zero? (length ys))) |
||
'() |
'() |
||
(fold append (map ( |
(fold append (map (lambda (x) (map (lambda (y) (list x y)) ys)) xs))))) |
||
(define nary-cartesian-product ( |
(define nary-cartesian-product (lambda (ls) |
||
(if (fold ( |
(if (fold (lambda (a b) (or a b)) (map (compose zero? length) ls)) |
||
'() |
'() |
||
(map flatten (fold cartesian-product ls))))) |
(map flatten (fold cartesian-product ls))))) |