For the Compleat Fan
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Mon Sep 25, 2006 11:46 am
is there a way of swapping two symbols' values?
Code: Select all
(set 'x 1 'y 2)
(? x y)
; x is 2, y is 1
without having to do this:
Fanda
Posts: 253 Joined: Tue Aug 02, 2005 6:40 am
Contact:
Post
by Fanda » Mon Sep 25, 2006 6:49 pm
I haven't tested speed, but this works:
Code: Select all
> (set 'x 1 'y 2)
2
> (map set '(x y) (list y x))
(2 1)
> x
2
> y
1
You can also put it in a macro:
Code: Select all
> (define-macro (exchange) (map set (args) (map eval (rotate (args) -1))))
(lambda-macro () (map set (args) (map eval (rotate (args) -1))))
> (set 'a 1 'b 2 'c 3 'd 4)
4
> (exchange a b c d)
(2 3 4 1)
> a
2
> b
3
> c
4
> d
1
Fanda
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Mon Sep 25, 2006 7:37 pm
Nice solutions, thanks Fanda!
I assume there's no parallel assignments in newLISP 'out of the box' then?
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Sun Oct 01, 2006 8:29 am
You can do this:
Code: Select all
(set 'x 1 'y 2)
; x is 1, y is 2
(swap x y)
; x is 2, y is 1
as of 8.9.13.
It pays to read the release notes!
Thanks Lutz - I like the way you grant my wishes...!