From Newsgroup: comp.lang.lisp
Frank Buss wrote:
I came across a small Logo example function on Brian Harvey's site,
that I translated into a few languages for comparison: http://lojic.com/blog/2007/08/31/logo-ruby-javascript
BTW: if you like short solutions, which looks more like a mathematical description of the problem and which doesn't bother with implementation details how to solve it, you should try Haskell:
choices [] = [[]]
choices (x:xs) = [ item:list | item <- x, list <- (choices xs) ]
It's somewhat shorter in Gauche Scheme.
(define choices cartesian-product)
In context:
(use util.combinations) ;; cartesian-product
(define choices cartesian-product)
(choices '(("small" "medium" "large")
("vanilla" "chocolate" "ginger")
("cone" "cup")))
(("small" "vanilla" "cone") ("small" "vanilla" "cup")
("small" "chocolate" "cone") ("small" "chocolate" "cup")
("small" "ginger" "cone") ("small" "ginger" "cup")
("medium" "vanilla" "cone") ("medium" "vanilla" "cup")
("medium" "chocolate" "cone") ("medium" "chocolate" "cup")
("medium" "ginger" "cone") ("medium" "ginger" "cup")
("large" "vanilla" "cone") ("large" "vanilla" "cup")
("large" "chocolate" "cone") ("large" "chocolate" "cup")
("large" "ginger" "cone") ("large" "ginger" "cup"))
Another way.
(define (map. func obj seq) (map (lambda(x) (func obj x)) seq))
(define (*L A B) (append-map (^a (map. cons a B)) A))
(define (choices List)
(if (null? List) '(()) (*L (car List) (choices (cdr List)))))
--
[T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
--- Kenny Tilton
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2