mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Use idiomatic quoting for identifiers.
This commit is contained in:
parent
3715c5c2b7
commit
cfc136dea8
@ -101,7 +101,7 @@ some-var ; => 5
|
|||||||
;; Accessing a previously unassigned variable is an exception
|
;; Accessing a previously unassigned variable is an exception
|
||||||
; x ; => x: undefined ...
|
; x ; => x: undefined ...
|
||||||
|
|
||||||
;; Local binding: me is bound to "Bob" only within (let ...)
|
;; Local binding: `me' is bound to "Bob" only within the (let ...)
|
||||||
(let ([me "Bob"])
|
(let ([me "Bob"])
|
||||||
"Alice"
|
"Alice"
|
||||||
me) ; => "Bob"
|
me) ; => "Bob"
|
||||||
@ -119,7 +119,7 @@ my-pet ; => #<dog>
|
|||||||
(dog-name my-pet) ; => "lassie"
|
(dog-name my-pet) ; => "lassie"
|
||||||
|
|
||||||
;;; Pairs (immutable)
|
;;; Pairs (immutable)
|
||||||
;; "cons" constructs pairs, "car" and "cdr" extract the first
|
;; `cons' constructs pairs, `car' and `cdr' extract the first
|
||||||
;; and second elements
|
;; and second elements
|
||||||
(cons 1 2) ; => '(1 . 2)
|
(cons 1 2) ; => '(1 . 2)
|
||||||
(car (cons 1 2)) ; => 1
|
(car (cons 1 2)) ; => 1
|
||||||
@ -130,10 +130,10 @@ my-pet ; => #<dog>
|
|||||||
;; Lists are linked-list data structures
|
;; Lists are linked-list data structures
|
||||||
(list 1 2 3) ; => '(1 2 3)
|
(list 1 2 3) ; => '(1 2 3)
|
||||||
|
|
||||||
;; Use "cons" to add an item to the beginning of a list
|
;; Use `cons' to add an item to the beginning of a list
|
||||||
(cons 4 '(1 2 3)) ; => (4 1 2 3)
|
(cons 4 '(1 2 3)) ; => (4 1 2 3)
|
||||||
|
|
||||||
;; Use "append" to add lists together
|
;; Use `append' to add lists together
|
||||||
(append '(1 2) '(3 4)) ; => (1 2 3 4)
|
(append '(1 2) '(3 4)) ; => (1 2 3 4)
|
||||||
|
|
||||||
;;; Vectors
|
;;; Vectors
|
||||||
@ -141,7 +141,7 @@ my-pet ; => #<dog>
|
|||||||
;; Vectors are fixed-length arrays
|
;; Vectors are fixed-length arrays
|
||||||
#(1 2 3) ; => '#(1 2 3)
|
#(1 2 3) ; => '#(1 2 3)
|
||||||
|
|
||||||
;; Use "vector-append" to add vectors together
|
;; Use `vector-append' to add vectors together
|
||||||
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
|
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
|
||||||
|
|
||||||
;;; Sets
|
;;; Sets
|
||||||
@ -149,13 +149,13 @@ my-pet ; => #<dog>
|
|||||||
;; 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)
|
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)
|
||||||
|
|
||||||
;; Add a member with "set-add"
|
;; Add a member with `set-add'
|
||||||
(set-add (set 1 2 3) 4); => (set 1 2 3 4)
|
(set-add (set 1 2 3) 4); => (set 1 2 3 4)
|
||||||
|
|
||||||
;; Remove one with "set-remove"
|
;; Remove one with `set-remove'
|
||||||
(set-remove (set 1 2 3) 1) ; => (set 2 3)
|
(set-remove (set 1 2 3) 1) ; => (set 2 3)
|
||||||
|
|
||||||
;; Test for existence with "set-member?"
|
;; Test for existence with `set-member?'
|
||||||
(set-member? (set 1 2 3) 1) ; => #t
|
(set-member? (set 1 2 3) 1) ; => #t
|
||||||
(set-member? (set 1 2 3) 4) ; => #f
|
(set-member? (set 1 2 3) 4) ; => #f
|
||||||
|
|
||||||
@ -173,14 +173,14 @@ my-pet ; => #<dog>
|
|||||||
;; You can provide a default value for missing keys
|
;; You can provide a default value for missing keys
|
||||||
(hash-ref m 'd 0) ; => 0
|
(hash-ref m 'd 0) ; => 0
|
||||||
|
|
||||||
;; Use "hash-set" to extend a hash table
|
;; Use `hash-set' to extend a hash table
|
||||||
(define m2 (hash-set m 'd 4))
|
(define m2 (hash-set m 'd 4))
|
||||||
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))
|
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))
|
||||||
|
|
||||||
;; Remember, these hashes are immutable!
|
;; Remember, these hashes are immutable!
|
||||||
m ; => '#hash((b . 2) (a . 1) (c . 3))
|
m ; => '#hash((b . 2) (a . 1) (c . 3))
|
||||||
|
|
||||||
;; Use "hash-remove" to remove keys
|
;; Use `hash-remove' to remove keys
|
||||||
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))
|
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
@ -230,16 +230,16 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;; 4. Equality
|
;; 4. Equality
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
;; for numbers use "="
|
;; for numbers use `='
|
||||||
(= 3 3.0) ; => #t
|
(= 3 3.0) ; => #t
|
||||||
(= 2 1) ; => #f
|
(= 2 1) ; => #f
|
||||||
|
|
||||||
;; for object identity use "eq?"
|
;; for object identity use `eq?'
|
||||||
(eq? 3 3) ; => #t
|
(eq? 3 3) ; => #t
|
||||||
(eq? 3 3.0) ; => #f
|
(eq? 3 3.0) ; => #f
|
||||||
(eq? (list 3) (list 3)) ; => #f
|
(eq? (list 3) (list 3)) ; => #f
|
||||||
|
|
||||||
;; for collections use "equal?"
|
;; for collections use `equal?'
|
||||||
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
|
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
|
||||||
(equal? (list 'a 'b) (list 'b 'a)) ; => #f
|
(equal? (list 'a 'b) (list 'b 'a)) ; => #f
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
'nope)
|
'nope)
|
||||||
; => 'yep
|
; => 'yep
|
||||||
|
|
||||||
;; "cond" chains a series of tests to select a result
|
;; `cond' chains a series of tests to select a result
|
||||||
(cond [(> 2 2) (error "wrong!")]
|
(cond [(> 2 2) (error "wrong!")]
|
||||||
[(< 2 2) (error "wrong again!")]
|
[(< 2 2) (error "wrong again!")]
|
||||||
[else 'ok]) ; => 'ok
|
[else 'ok]) ; => 'ok
|
||||||
@ -305,13 +305,13 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(for/hash ([i '(1 2 3)])
|
(for/hash ([i '(1 2 3)])
|
||||||
(values i (number->string i))) ; => '#hash((1 . "1") (2 . "2") (3 . "3"))
|
(values i (number->string i))) ; => '#hash((1 . "1") (2 . "2") (3 . "3"))
|
||||||
|
|
||||||
;; To combine iteration results, use "for/fold"
|
;; To combine iteration results, use `for/fold'
|
||||||
(for/fold ([sum 0]) ([i '(1 2 3 4)])
|
(for/fold ([sum 0]) ([i '(1 2 3 4)])
|
||||||
(+ sum i)) ; => 10
|
(+ sum i)) ; => 10
|
||||||
|
|
||||||
;;; Sequences
|
;;; Sequences
|
||||||
|
|
||||||
;; "for" allows iteration over sequences:
|
;; `for' allows iteration over sequences:
|
||||||
;; lists, vectors, strings, sets, hash tables, etc...
|
;; lists, vectors, strings, sets, hash tables, etc...
|
||||||
(for ([i (in-list '(l i s t))])
|
(for ([i (in-list '(l i s t))])
|
||||||
(displayln i))
|
(displayln i))
|
||||||
@ -330,8 +330,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
|
|
||||||
;;; Exceptions
|
;;; Exceptions
|
||||||
|
|
||||||
;; To catch an exception, use the "with-handlers" form
|
;; To catch an exception, use the `with-handlers' form
|
||||||
;; To throw an exception use "raise"
|
;; To throw an exception use `raise'
|
||||||
(with-handlers
|
(with-handlers
|
||||||
([(lambda (v) (equal? v "infinity"))
|
([(lambda (v) (equal? v "infinity"))
|
||||||
(lambda (exn) +inf.0)])
|
(lambda (exn) +inf.0)])
|
||||||
@ -349,7 +349,7 @@ 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...)
|
||||||
|
|
||||||
;; Use "vector" to create a mutable vector
|
;; Use `vector' to create a mutable vector
|
||||||
(define vec (vector 2 2 3 4))
|
(define vec (vector 2 2 3 4))
|
||||||
;; Use vector-set! to update a slot
|
;; Use vector-set! to update a slot
|
||||||
(vector-set! vec 0 1)
|
(vector-set! vec 0 1)
|
||||||
@ -375,10 +375,10 @@ vec ; => #(1 2 3 4)
|
|||||||
(printf fmt (make-string n ch))
|
(printf fmt (make-string n ch))
|
||||||
(newline)))
|
(newline)))
|
||||||
|
|
||||||
;; Use "require" to import all functions from the module
|
;; Use `require' to import all functions from the module
|
||||||
(require 'cake)
|
(require 'cake)
|
||||||
(print-cake 3)
|
(print-cake 3)
|
||||||
; (show "~a" 1 #\A) ; => error, "show" was not exported
|
; (show "~a" 1 #\A) ; => error, `show' was not exported
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 8. Classes and Objects
|
;; 8. Classes and Objects
|
||||||
@ -400,7 +400,7 @@ vec ; => #(1 2 3 4)
|
|||||||
(define charlie
|
(define charlie
|
||||||
(new fish% [size 10]))
|
(new fish% [size 10]))
|
||||||
|
|
||||||
;; Use "send" to call an object's methods
|
;; Use `send' to call an object's methods
|
||||||
(send charlie grow 6)
|
(send charlie grow 6)
|
||||||
(send charlie get-size) ; => 16
|
(send charlie get-size) ; => 16
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user