[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Inverting alists -- *NOT* a homework assignment!!

William James

5/11/2015 8:40:00 AM

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 argument)
(append-map
(lambda (xs)
(map
(cut list <> (car xs))
(cdr xs)))
argument))

(invert-alist
'((reference-1 referent-a referent-b) (reference-2 referent-c)))
===>
((referent-a reference-1) (referent-b reference-1) (referent-c reference-2))


Another way:

(use srfi-42 :only (list-ec))

(define (invert-alist argument)
(list-ec
(: xs argument)
(: x (cdr xs))
(list x (car xs))))

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... I think there's a billion Africans now.
They've already said that everybody from Syria can come to Sweden because they
have a civil war there.... They have a huge housing crisis; they are actually
thinking of commandeering people's vacation homes because they need more housing
for immigrants. --- Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)
2 Answers

William James

12/1/2015 6:02:00 PM

0

WJ wrote:

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

Ocaml:

open List;;

let invert_alist alist =
alist
|> map (function x::xs -> List.map (fun y -> [y;x]) xs)
|> flatten ;;

invert_alist
[["reference-1";"referent-a";"referent-b"];["reference-2";"referent-c"]];;

===>
[["referent-a"; "reference-1"];
["referent-b"; "reference-1"];
["referent-c"; "reference-2"]]

--
It is one of the highest offices of constitutions to protect the rights and the
liberty of the citizen. We may say that if a constitution fails in this, it
fails in all. Under all forms of government the greatest danger to the citizen
is from those who govern. --- The Old Guard, February 1863, p. 31

William James

1/20/2016 9:35:00 PM

0

WJ wrote:

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

MatzLisp (Ruby):

def invert_alist a
a.flat_map{|k,*vs| vs.map{|v| [v,k]}}
end

invert_alist [[:a,0,2],[:b,3,5,7]]
==>[[0, :a], [2, :a], [3, :b], [5, :b], [7, :b]]

--
Use this [sword] for me, if I rule well; if not, against me. --- Trajan
"If a government uses the instruments of power in its hands for the purpose of
leading a people to ruin, then rebellion is not only the right but also the
duty of every individual citizen."