Page 1 of 1
Exchange symbols
Posted: Mon Sep 25, 2006 11:46 am
by cormullion
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:
Posted: Mon Sep 25, 2006 6:49 pm
by Fanda
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
Posted: Mon Sep 25, 2006 7:37 pm
by cormullion
Nice solutions, thanks Fanda!
I assume there's no parallel assignments in newLISP 'out of the box' then?
Posted: Sun Oct 01, 2006 8:29 am
by cormullion
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...!