mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 14:56:54 +02:00
added sets, hashes and objects
This commit is contained in:
@@ -13,10 +13,8 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
|
|
||||||
; TODO:
|
; TODO:
|
||||||
; quote
|
; quote
|
||||||
; collections (set, hash)
|
|
||||||
; structs
|
; structs
|
||||||
; control flow (pattern-matching, loops, sequences)
|
; control flow (pattern-matching, loops, sequences)
|
||||||
; objects
|
|
||||||
|
|
||||||
|
|
||||||
; Single line comments start with a semicolon
|
; Single line comments start with a semicolon
|
||||||
@@ -29,15 +27,17 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
;; 1. Primitive Datatypes and Operators
|
;; 1. Primitive Datatypes and Operators
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; You have numbers
|
; Numbers
|
||||||
9999999999999999999999 ; big integers
|
9999999999999999999999 ; integers
|
||||||
3.14 ; reals
|
3.14 ; reals
|
||||||
6.02e+23
|
6.02e+23
|
||||||
1/2 ; rationals
|
1/2 ; rationals
|
||||||
1+2i ; complex numbers
|
1+2i ; complex numbers
|
||||||
|
|
||||||
; Operators are functions, functions applications are written
|
; Function application is written (f x y z ...)
|
||||||
; (f x y z ...) where f is a function and x, y, z, ... are operands
|
; where f is a function and x, y, z, ... are operands
|
||||||
|
|
||||||
|
; Here are a few arithmetic operators
|
||||||
(+ 1 1) ; => 2
|
(+ 1 1) ; => 2
|
||||||
(- 8 1) ; => 7
|
(- 8 1) ; => 7
|
||||||
(* 10 2) ; => 20
|
(* 10 2) ; => 20
|
||||||
@@ -103,30 +103,65 @@ some-var ; => 6
|
|||||||
;; 3. Collections
|
;; 3. Collections
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Lists are linked-list data structures, vectors are fixed-length arrays.
|
; Lists are linked-list data structures
|
||||||
'(1 2 3) ; a list
|
'(1 2 3)
|
||||||
#(1 2 3) ; a vector
|
|
||||||
|
|
||||||
; Use cons to add an item to the beginning of a list
|
; Vectors are fixed-length arrays
|
||||||
|
#(1 2 3)
|
||||||
|
|
||||||
|
; 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)
|
||||||
|
|
||||||
; Use filter, map to interact with collections
|
; Use "filter", "map" to interact with collections
|
||||||
(map add1 '(1 2 3)) ; => (2 3 4)
|
(map add1 '(1 2 3)) ; => (2 3 4)
|
||||||
(filter even? '(1 2 3)) ; => (2)
|
(filter even? '(1 2 3)) ; => (2)
|
||||||
|
|
||||||
; Use fold to reduce them
|
; Use "fold" to reduce them
|
||||||
(foldl + 0 '(1 2 3 4))
|
(foldl + 0 '(1 2 3 4))
|
||||||
; = (+ 1 (+ 2 (+ 3 (+ 4 0)))
|
; = (+ 1 (+ 2 (+ 3 (+ 4 0)))
|
||||||
; => 10
|
; => 10
|
||||||
|
|
||||||
; Set
|
;;; Sets
|
||||||
|
|
||||||
|
; create a set from a list
|
||||||
|
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)
|
||||||
|
|
||||||
; Hash
|
; Add a member with "set-add"
|
||||||
|
(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)
|
||||||
|
|
||||||
|
; Test for existence with "set-member?"
|
||||||
|
(set-member? (set 1 2 3) 1) ; => #t
|
||||||
|
(set-member? (set 1 2 3) 4) ; => #f
|
||||||
|
|
||||||
|
;;; Hashes
|
||||||
|
|
||||||
|
; Create an immutable hash table (There are also mutables ones)
|
||||||
|
(define m (hash 'a 1 'b 2 'c 3))
|
||||||
|
|
||||||
|
; Retrieve a value
|
||||||
|
(hash-ref m 'a) ; => 1
|
||||||
|
|
||||||
|
; Retrieving a non-present value is an exception
|
||||||
|
; (hash-ref m 'd) => no value found
|
||||||
|
|
||||||
|
; You can provide a default value for missing keys
|
||||||
|
(hash-ref m 'd 0) ; => 0
|
||||||
|
|
||||||
|
; Use "hash-set" to extend a hash table
|
||||||
|
(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))
|
||||||
|
|
||||||
|
; Use "hash-remove" to remove keys
|
||||||
|
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 3. Functions
|
;; 3. Functions
|
||||||
@@ -174,6 +209,7 @@ some-var ; => 6
|
|||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 4. Control Flow
|
;; 4. Control Flow
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Conditionals
|
; Conditionals
|
||||||
(if #t ; test expression
|
(if #t ; test expression
|
||||||
"this is true" ; then expression
|
"this is true" ; then expression
|
||||||
@@ -187,7 +223,7 @@ some-var ; => 6
|
|||||||
'nope) ; => 'yep
|
'nope) ; => 'yep
|
||||||
|
|
||||||
|
|
||||||
; Cond chains a series of test to select a result
|
; "cond" chains a series of tests to select a result
|
||||||
(cond
|
(cond
|
||||||
[(> 2 2) (error "wrong!")]
|
[(> 2 2) (error "wrong!")]
|
||||||
[(< 2 2) (error "wrong again!")]
|
[(< 2 2) (error "wrong again!")]
|
||||||
@@ -200,8 +236,8 @@ some-var ; => 6
|
|||||||
; Sequences
|
; Sequences
|
||||||
|
|
||||||
; 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)])
|
||||||
@@ -226,14 +262,42 @@ some-var ; => 6
|
|||||||
(printf fmt (make-string n ch))
|
(printf fmt (make-string n ch))
|
||||||
(newline)))
|
(newline)))
|
||||||
|
|
||||||
(require 'cake) ;; import all 'cake' functions
|
;; Use "require" to import all functions from the module
|
||||||
|
(require 'cake)
|
||||||
(print-cake 3)
|
(print-cake 3)
|
||||||
;(show "~a" 1 #\A) ; => this is an error
|
;(show "~a" 1 #\A) ; => error, "show" was not exported
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 6. Classes
|
;; 6. Classes and Objects
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
; Create a class fish%
|
||||||
|
(define fish%
|
||||||
|
(class object%
|
||||||
|
(init size) ; initialization argument
|
||||||
|
(super-new) ; superclass initialization
|
||||||
|
|
||||||
|
(define current-size size) ; field
|
||||||
|
|
||||||
|
; Public methods
|
||||||
|
(define/public (get-size)
|
||||||
|
current-size)
|
||||||
|
|
||||||
|
(define/public (grow amt)
|
||||||
|
(set! current-size (+ amt current-size)))
|
||||||
|
|
||||||
|
(define/public (eat other-fish)
|
||||||
|
(grow (send other-fish get-size)))))
|
||||||
|
|
||||||
|
|
||||||
|
; Create an instance of fish%
|
||||||
|
(define charlie
|
||||||
|
(new fish% [size 10]))
|
||||||
|
|
||||||
|
; Use "send" to call an object's methods
|
||||||
|
(send charlie grow 6)
|
||||||
|
(send charlie get-size) ; => 16
|
||||||
|
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 7. Macros
|
;; 7. Macros
|
||||||
@@ -258,7 +322,7 @@ some-var ; => 6
|
|||||||
(printf "tmp = ~a; a = ~a; b = ~a" tmp a b) ; tmp is unaffected by swap
|
(printf "tmp = ~a; a = ~a; b = ~a" tmp a b) ; tmp is unaffected by swap
|
||||||
```
|
```
|
||||||
|
|
||||||
;; Further Reading
|
## Further Reading
|
||||||
|
|
||||||
Still up for more? Try [Quick: An Introduction to Racket with Pictures](http://docs.racket-lang.org/quick/)
|
Still up for more? Try [Quick: An Introduction to Racket with Pictures](http://docs.racket-lang.org/quick/)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user