summaryrefslogtreecommitdiffhomepage
path: root/racket.html.markdown
diff options
context:
space:
mode:
authorEli Barzilay <eli@barzilay.org>2013-07-16 02:08:28 -0400
committerEli Barzilay <eli@barzilay.org>2013-07-16 02:08:28 -0400
commit159b7e4e1e845304bd8eb4398677010b86c7f249 (patch)
tree389f69f152ec7e59c4d34a830dbf0ae36acd3bb0 /racket.html.markdown
parent7a92ee7ab8833ba3871387613ce14eb958aee11a (diff)
Add boxes example.
Diffstat (limited to 'racket.html.markdown')
-rw-r--r--racket.html.markdown10
1 files changed, 8 insertions, 2 deletions
diff --git a/racket.html.markdown b/racket.html.markdown
index d43ac5e2..fbd3f76a 100644
--- a/racket.html.markdown
+++ b/racket.html.markdown
@@ -354,11 +354,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
;; 6. Mutation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Use set! to assign a new value to an existing variable
+;; Use `set!' to assign a new value to an existing variable
(define n 5)
-(set! n 6)
+(set! n (add1 n))
n ; => 6
+;; Use boxes for explicitly mutable values (similar to pointers or
+;; references in other languages)
+(define n* (box 5))
+(set-box! n* (add1 (unbox n*)))
+(unbox n*) ; => 6
+
;; Many Racket datatypes can be immutable or mutable
;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...)