mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-17 05:28:37 +01:00
Improve mutable/immutable comment, add `make-vector' example.
This commit is contained in:
parent
159b7e4e1e
commit
547a8a6db1
@ -365,13 +365,16 @@ n ; => 6
|
|||||||
(set-box! n* (add1 (unbox n*)))
|
(set-box! n* (add1 (unbox n*)))
|
||||||
(unbox n*) ; => 6
|
(unbox n*) ; => 6
|
||||||
|
|
||||||
;; Many Racket datatypes can be immutable or mutable
|
;; Many Racket datatypes are immutable (pairs, lists, etc), some come in
|
||||||
;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...)
|
;; both mutable and immutable flavors (strings, vectors, hash tables,
|
||||||
|
;; etc...)
|
||||||
|
|
||||||
;; Use `vector' to create a mutable vector
|
;; Use `vector' or `make-vector' to create mutable vectors
|
||||||
(define vec (vector 2 2 3 4))
|
(define vec (vector 2 2 3 4))
|
||||||
|
(define wall (make-vector 100 'bottle-of-beer))
|
||||||
;; Use vector-set! to update a slot
|
;; Use vector-set! to update a slot
|
||||||
(vector-set! vec 0 1)
|
(vector-set! vec 0 1)
|
||||||
|
(vector-set! wall 99 'down)
|
||||||
vec ; => #(1 2 3 4)
|
vec ; => #(1 2 3 4)
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user