Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

I was gratified to be able to answer promptly, and I did. I said I didn't know. -- Mark Twain


comp / comp.lang.lisp / Re: Inverting alists -- *NOT* a homework assignment!!

SubjectAuthor
o Re: Inverting alists -- *NOT* a homework assignment!!B. Pym

1
Subject: Re: Inverting alists -- *NOT* a homework assignment!!
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Thu, 19 Sep 2024 12:30 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Inverting alists -- *NOT* a homework assignment!!
Date: Thu, 19 Sep 2024 12:30:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <vch5hr$ijn1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 19 Sep 2024 14:30:54 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="9391f12034a3e9c710c0f52af14a006a";
logging-data="610017"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+z6+LBWGXkjnYkiNkXXkoc"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:EFA6/eF6X/lvPoe19igyK0x1sew=
View all headers

Christopher N. Vogt wrote:

> > ;; Hi. I would like to invert an alist of the form ;; ;; ((reference-1
> > referent-a referent-b...) (reference-2 referent-c ...)...) ;; ;; to
> > another alist of the form ;; ;; ((referent-a reference-1) (referent-b
> > reference-1) ;; (referent-c reference-2) ...) ;; ;; I have gotten
> > something to work but it's so stupefyingly *UGLY* that I ;; just know
> > someone out there can demonstrate a more better beautiful ;; elegant
> > means of achieving the same result. ;; ;; [ I'm working in emacs-lisp
> > with the 'cl' package, so the code below ;; ought to be reasonably close
> > to Common Lisp...] ;; ;; ;; Mind you, I am teaching myself lisp and THIS

.....

> I'd do it something like this:
> (defun invert-alist (alist)
> (loop for list in test
> for reference = (first list)
> appending (loop for referent in (cdr list)
> collect (list referent reference))))

Gauche Scheme

(define (invert-alist alist)
(@ append-map xs :
(map (cut list <> (car xs)) (cdr xs)) :
alist))

Given:

(define-syntax @-aux
(syntax-rules (:)
;; No parameters given; use "cut".
[(_ func () ((e0 e ...)) : stuff ...)
(func (cut e0 e ...) stuff ...)]
[(_ func vars (e0 e ...) : stuff ...)
(func (lambda vars e0 e ...) stuff ...)]
[(_ func vars (e0 e ...) expr more ...)
(@-aux func vars (e0 e ... expr) more ...)]
[(_ func vars () : expr more ...)
(@-aux func vars (expr) more ...)]
[(_ func (vars ...) () var more ...)
(@-aux func (vars ... var) () more ...)]
))

(define-syntax @
(syntax-rules ()
[(_ func stuff ...)
(@-aux func () () stuff ...)]))

1

rocksolid light 0.9.8
clearnet tor