• Re: choices sample Logo -> Lisp

    From B. Pym@21:1/5 to B. Pym on Sat Aug 2 15:43:24 2025
    XPost: comp.lang.scheme

    B. Pym wrote:

    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)))))

    MatzLisp:

    def choices(arys)
    arys[0].product( *arys.drop(1) )
    end

    --
    [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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Frank Buss on Sat Aug 2 15:27:50 2025
    XPost: comp.lang.scheme

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)