Groups | Search | Server Info | Login | Register


Groups > comp.lang.scheme > #6534

Re: choices sample Logo -> Lisp

From "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups comp.lang.lisp, comp.lang.scheme
Subject Re: choices sample Logo -> Lisp
Date 2025-08-02 15:27 +0000
Organization A noiseless patient Spider
Message-ID <106lapl$14sub$1@dont-email.me> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


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

Back to comp.lang.scheme | Previous | NextNext in thread | Find similar


Thread

Re: choices sample Logo -> Lisp "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-02 15:27 +0000
  Re: choices sample Logo -> Lisp "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-02 15:43 +0000

csiph-web