1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-06 14:56:54 +02:00

swap' -> swap!'

This commit is contained in:
Eli Barzilay
2013-07-16 02:06:16 -04:00
parent fbd215521f
commit 7a92ee7ab8

View File

@@ -441,7 +441,7 @@ vec ; => #(1 2 3 4)
(set! i (add1 i)))) (set! i (add1 i))))
;; Macros are hygienic, you cannot clobber existing variables! ;; Macros are hygienic, you cannot clobber existing variables!
(define-syntax-rule (swap x y) (define-syntax-rule (swap! x y) ; -! is idomatic for mutation
(let ([tmp x]) (let ([tmp x])
(set! x y) (set! x y)
(set! y tmp))) (set! y tmp)))
@@ -449,7 +449,7 @@ vec ; => #(1 2 3 4)
(define tmp 1) (define tmp 1)
(define a 2) (define a 2)
(define b 3) (define b 3)
(swap a b) (swap! a b)
(printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected (printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;