Path: ...!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 10 Aug 2024 17:33:15 +0000 From: steve g Newsgroups: comp.lang.lisp Subject: Re: .Re: simple lisp function References: <806cc1215afc0176956ea8bfdb3cbcff@www.novabbs.com> Date: Sat, 10 Aug 2024 13:33:15 -0400 Message-ID: <87bk20ffl0.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:lfB+iibynsVbmrSpMCm9u2KWnZI= MIME-Version: 1.0 Content-Type: text/plain Lines: 51 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-pm30bw+OwFLIMXgaBifPL+6kE9APkRSokme61F8UEBp/vh1KQ3kRW7UindaS6ni39nnXiWng4sohUvI!I+uGitODtmmYCQjG2BUmIyTJ/haOTGPicf1BLV8ypZk8IK8= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Bytes: 2691 HenHanna 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))