diff options
author | Eli Barzilay <eli@barzilay.org> | 2013-07-16 01:55:29 -0400 |
---|---|---|
committer | Eli Barzilay <eli@barzilay.org> | 2013-07-16 01:55:29 -0400 |
commit | 6af5c957d039eb4712092dcaf1e531ae4e9a35f9 (patch) | |
tree | 05ead1d45c61009e5efc248f904755eeff31c31a /racket.html.markdown | |
parent | 5d952fb6dab4989454c7e2e43fac664180817df8 (diff) |
Clarify functional data structures.
Note that `set-add' is functional, clarify immutable hashes, add mutable
hash examples to contrast.
Diffstat (limited to 'racket.html.markdown')
-rw-r--r-- | racket.html.markdown | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/racket.html.markdown b/racket.html.markdown index 21769e90..7ae9dad0 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -97,7 +97,7 @@ some-var ; => 5 ;; You can also use unicode characters (define ⊆ subset?) -(⊆ (set 3 2) (set 1 2 3)); => #t +(⊆ (set 3 2) (set 1 2 3)) ; => #t ;; Accessing a previously unassigned variable is an exception ; x ; => x: undefined ... @@ -147,11 +147,12 @@ my-pet ; => #<dog> ;;; Sets -;; create a set from a list +;; Create a set from a list (list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3) ;; Add a member with `set-add' -(set-add (set 1 2 3) 4); => (set 1 2 3 4) +;; (Functional: returns the extended set rather than mutate the input) +(set-add (set 1 2 3) 4) ; => (set 1 2 3 4) ;; Remove one with `set-remove' (set-remove (set 1 2 3) 1) ; => (set 2 3) @@ -162,7 +163,7 @@ my-pet ; => #<dog> ;;; Hashes -;; Create an immutable hash table (There are also mutables ones) +;; Create an immutable hash table (mutable example below) (define m (hash 'a 1 'b 2 'c 3)) ;; Retrieve a value @@ -174,16 +175,26 @@ my-pet ; => #<dog> ;; You can provide a default value for missing keys (hash-ref m 'd 0) ; => 0 -;; Use `hash-set' to extend a hash table +;; Use `hash-set' to extend an immutable hash table +;; (Returns the extended hash instdead of mutating it) (define m2 (hash-set m 'd 4)) m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3)) ;; Remember, these hashes are immutable! -m ; => '#hash((b . 2) (a . 1) (c . 3)) +m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' -;; Use `hash-remove' to remove keys +;; Use `hash-remove' to remove keys (functional too) (hash-remove m 'a) ; => '#hash((b . 2) (c . 3)) +;; Create an empty mutable hash table and manipulate it +(define m3 (make-hash)) +(hash-set! m3 'a 1) +(hash-set! m3 'b 2) +(hash-set! m3 'c 3) +(hash-ref m3 'a) ; => 1 +(hash-ref m3 'd 0) ; => 0 +(hash-remove! m3 'a) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |