• Re: simple lisp function

    From B. Pym@21:1/5 to Pascal Bourguignon on Fri Jul 4 12:37:40 2025
    XPost: comp.lang.lisp

    Pascal Bourguignon wrote:

    this simple function i'm trying to write is giving me headaches!
    basically i want to do something that does:
    (variations 'x '(y z)) -> ((x y z) (y x z) (y z x))

    i come from a procedural programming background and find functional programming very confusing (especially recursion). can someone give
    me some hints? my attempts at this make no sense so pasting them here would only confirm my newbish forray into LSIP. thanks for any help!

    (defun variations (item list)
    (if (null list)
    (list (list item))
    (cons (cons item list)
    (mapcar (lambda (rest) (cons (car list) rest))
    (variations item (cdr list))))))

    Peter Seibel wrote:

    (defun variations (x list)
    (loop for cons on (cons nil list) collecting
    (nconc (ldiff list (cdr cons)) (cons x (cdr cons)))))

    Okay, so that's arguably obfuscated Lisp. But you should never pass up
    a chance to combine LOOP, LDIFF, and abuse of Common Lisp's Lisp-2
    nature.

    Scheme

    "We don't need no stinkin' loops!"

    (define (variations e x . y)
    (cons `(,@y,e,@x)
    (if (null? x)
    '()
    (apply variations e (cdr x) `(,@y,(car x))))))

    (variations '- '(a b c))
    ===>
    ((- a b c) (a - b c) (a b - c) (a b c -))

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