Swap Without Temporary Variable.

; Short one, inspired by blog post posted by Cormullion on Newlisp forum:
; Swap values of two variables without using temporary variable.

(set 'a 1 'b 2)

(println a "," b); 1, 2

(set 'a (+ a b))
(set 'b (- a b))
(set 'a (- a b))

(println a "," b); 2, 1

(exit)

3 comments:

  1. (set 'a 1 'b 2)

    (println a "," b); 1, 2

    (map set '(a b) (list b a))

    (println a "," b); 2, 1

    ReplyDelete
  2. Great idea - works with everything, not just numbers.

    ReplyDelete