Sujet : Re: .Re: simple lisp function
De : sgonedes1977 (at) *nospam* gmail.com (steve g)
Groupes : comp.lang.lispDate : 10. Aug 2024, 19:33:15
Autres entêtes
Message-ID : <87bk20ffl0.fsf@gmail.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13)
HenHanna <
HenHanna@dev.null> writes:
B. Pym wrote:
>
< > 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))))))
>
< > Gauche Scheme:
>
< > (use srfi-1) ;; split-at
< > (use srfi-42) ;; list-ec
>
< > (define (variations x lst)
< > (list-ec (: i (+ 1 (length lst)))
< > (receive (a b) (split-at lst i)
< > `(,@a ,x ,@b))))
>
< > (variations '- '(a b c)) ===> ((- a b c) (a - b c) (a b - c) (a b
< > c -))
< > (variations '- '(a)) ===> ((- a) (a -))
< > (variations '- '()) ===> ((-))
>
>
I remember writing this in Lisp and Python. a few years ago.
>
>
Is Scheme (or Gauche) used outside of the Academia? What kind of people
(other than Grad students, Researchers in
Prog.Lang design)
would know about Split-at and List-ec and Receive ?
lisp programmers?
CL-USER> (mapcon #'list '(X 1 2 3 4))
((X 1 2 3 4) (1 2 3 4) (2 3 4) (3 4) (4))