Re: simple lisp function

Liste des GroupesRevenir à cl lisp 
Sujet : Re: simple lisp function
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 04. Jul 2025, 13:37:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <1048huj$qp3e$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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 -))

Date Sujet#  Auteur
4 Jul13:37 o Re: simple lisp function1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal