Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 49:51:10 |
Calls: | 583 |
Files: | 1,138 |
Messages: | 111,303 |
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) ]
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)))))