Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

No amount of careful planning will ever replace dumb luck.


devel / comp.lang.lisp / .Re: simple lisp function

SubjectAuthor
o .Re: simple lisp functionRobert L.

1
.Re: simple lisp function

<sv27np$1a57$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16915&group=comp.lang.lisp#16915

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!EddJMpWpO0oBY4q796VQYg.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: simple lisp function
Date: Tue, 22 Feb 2022 08:43:39 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <sv27np$1a57$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="43175"; posting-host="EddJMpWpO0oBY4q796VQYg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Tue, 22 Feb 2022 08:43 UTC

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

A monkey (Peter Seibel <peter@javamonkey.com>) 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.

By using a Lisp instead of CL (COBOL-Like), we can make it shorter.

Gauche Scheme or Racket:

(require srfi/1) ;; iota for Racket

(define (variations x seq)
(map (lambda (i) `(,@(take seq i) ,x ,@(drop seq i)))
(iota (+ 1 (length seq)))))

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


devel / comp.lang.lisp / .Re: simple lisp function

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor