mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Add boxes example.
This commit is contained in:
parent
7a92ee7ab8
commit
159b7e4e1e
@ -354,11 +354,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
|
|||||||
;; 6. Mutation
|
;; 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)
|
(define n 5)
|
||||||
(set! n 6)
|
(set! n (add1 n))
|
||||||
n ; => 6
|
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
|
;; Many Racket datatypes can be immutable or mutable
|
||||||
;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...)
|
;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user