mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
corrections and suggestions
This commit is contained in:
parent
150b232b9b
commit
13604e7932
@ -11,11 +11,19 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
```racket
|
```racket
|
||||||
#lang racket ; defines the language we are using
|
#lang racket ; defines the language we are using
|
||||||
|
|
||||||
|
;;; Comments
|
||||||
|
|
||||||
; Single line comments start with a semicolon
|
; Single line comments start with a semicolon
|
||||||
#| Multiline strings can be written
|
|
||||||
using three "'s, and are often used
|
#| Block comments
|
||||||
as comments
|
can span multiple lines and...
|
||||||
|
#|
|
||||||
|
they can be nested !
|
||||||
|#
|
|#
|
||||||
|
|#
|
||||||
|
|
||||||
|
; S-expression comments discard the following expression
|
||||||
|
#; "this expression will be discarded" "2nd expression" ; => "2nd expression"
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 1. Primitive Datatypes and Operators
|
;; 1. Primitive Datatypes and Operators
|
||||||
@ -46,12 +54,10 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
|
|
||||||
;;; Booleans
|
;;; Booleans
|
||||||
#t ; for true
|
#t ; for true
|
||||||
#f ; for false
|
#f ; for false -- any value other than #f is true
|
||||||
(not #t) ; => #f
|
(not #t) ; => #f
|
||||||
|
(and 0 #f (error "doesn't get here")) ; => #f
|
||||||
; Equality for numbers is =
|
(or #f 0 (error "doesn't get here")) ; => 0
|
||||||
(= 1 1.0) ; => #t
|
|
||||||
(= 2 1) ; => #f
|
|
||||||
|
|
||||||
;;; Characters
|
;;; Characters
|
||||||
#\A ; => #\A
|
#\A ; => #\A
|
||||||
@ -83,9 +89,9 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
(define some-var 5)
|
(define some-var 5)
|
||||||
some-var ; => 5
|
some-var ; => 5
|
||||||
|
|
||||||
; Use set! to assign a new value to an existing variable
|
; You can also use unicode characters
|
||||||
(set! some-var 6)
|
(define ⊆ subset?)
|
||||||
some-var ; => 6
|
(⊆ (set 3 2) (set 1 2 3)); => #t
|
||||||
|
|
||||||
; Accessing a previously unassigned variable is an exception
|
; Accessing a previously unassigned variable is an exception
|
||||||
;x ; => x: undefined ...
|
;x ; => x: undefined ...
|
||||||
@ -107,18 +113,17 @@ my-pet ; => #<dog>
|
|||||||
(dog? my-pet) ; => #t
|
(dog? my-pet) ; => #t
|
||||||
(dog-name my-pet) ; => "lassie"
|
(dog-name my-pet) ; => "lassie"
|
||||||
|
|
||||||
; Pairs
|
;;; 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
|
||||||
(cdr (cons 1 2)) ; => 2
|
(cdr (cons 1 2)) ; => 2
|
||||||
|
|
||||||
; Lists are linked-list data structures
|
;;; Lists
|
||||||
'(1 2 3) ; => '(1 2 3)
|
|
||||||
|
|
||||||
; Vectors are fixed-length arrays
|
; Lists are linked-list data structures
|
||||||
#(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)
|
||||||
@ -126,14 +131,13 @@ my-pet ; => #<dog>
|
|||||||
; 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
|
;;; Vectors
|
||||||
(map add1 '(1 2 3)) ; => (2 3 4)
|
|
||||||
(filter even? '(1 2 3)) ; => (2)
|
|
||||||
|
|
||||||
; Use "fold" to reduce them
|
; Vectors are fixed-length arrays
|
||||||
(foldl + 0 '(1 2 3 4))
|
#(1 2 3) ; => '#(1 2 3)
|
||||||
; = (+ 1 (+ 2 (+ 3 (+ 4 0)))
|
|
||||||
; => 10
|
; Use "vector-append" to add vectors together
|
||||||
|
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
|
||||||
|
|
||||||
;;; Sets
|
;;; Sets
|
||||||
|
|
||||||
@ -218,7 +222,24 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
; => "Hello Finn, you passed 3 extra args"
|
; => "Hello Finn, you passed 3 extra args"
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 4. Control Flow
|
;; 4. Equality
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
; for numbers use "="
|
||||||
|
(= 3 3.0) ; => #t
|
||||||
|
(= 2 1) ; => #f
|
||||||
|
|
||||||
|
; for object identity use "eq?"
|
||||||
|
(eq? 3 3) ; => #t
|
||||||
|
(eq? 3 3.0) ; => #f
|
||||||
|
(eq? (list 3) (list 3)) ; => #f
|
||||||
|
|
||||||
|
; for collections use "equal?"
|
||||||
|
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
|
||||||
|
(equal? (list 'a 'b) (list 'b 'a)) ; => #f
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; 5. Control Flow
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
;;; Conditionals
|
;;; Conditionals
|
||||||
@ -254,6 +275,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
|
|
||||||
;;; Loops
|
;;; Loops
|
||||||
|
|
||||||
|
; looping can be done through recursion
|
||||||
(define (loop i)
|
(define (loop i)
|
||||||
(when (< i 10)
|
(when (< i 10)
|
||||||
(printf "i:~a~n" i)
|
(printf "i:~a~n" i)
|
||||||
@ -261,11 +283,26 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
|
|
||||||
(loop 5) ; => i:5 i:6 ...
|
(loop 5) ; => i:5 i:6 ...
|
||||||
|
|
||||||
|
; similarly, with a named let
|
||||||
(let loop ((i 0))
|
(let loop ((i 0))
|
||||||
(when (< i 10)
|
(when (< i 10)
|
||||||
(printf "i:~a~n" i)
|
(printf "i:~a~n" i)
|
||||||
(loop (add1 i)))) ; => i:0 i:1 ...
|
(loop (add1 i)))) ; => i:0 i:1 ...
|
||||||
|
|
||||||
|
;;; Comprehensions
|
||||||
|
|
||||||
|
(for/list ([i '(1 2 3)])
|
||||||
|
(add1 i)) ; => '(2 3 4)
|
||||||
|
|
||||||
|
(for/list ([i '(1 2 3)] #:when (even? i))
|
||||||
|
i) ; => '(2)
|
||||||
|
|
||||||
|
(for/hash ([i '(1 2 3)])
|
||||||
|
(values i (number->string i))) ; => '#hash((1 . "1") (2 . "2") (3 . "3"))
|
||||||
|
|
||||||
|
; To combine iteration results, use "for/fold"
|
||||||
|
(for/fold ([sum 0]) ([i '(1 2 3 4)])
|
||||||
|
(+ sum i)) ; => 10
|
||||||
|
|
||||||
;;; Sequences
|
;;; Sequences
|
||||||
|
|
||||||
@ -286,7 +323,6 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
|
(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))])
|
||||||
(printf "key:~a value:~a ~n" k v))
|
(printf "key:~a value:~a ~n" k v))
|
||||||
|
|
||||||
|
|
||||||
;;; Exceptions
|
;;; Exceptions
|
||||||
|
|
||||||
; To catch an exception, use the "with-handlers" form
|
; To catch an exception, use the "with-handlers" form
|
||||||
@ -297,7 +333,25 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(raise "infinity"))
|
(raise "infinity"))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 5. Modules
|
;; 6. Mutation
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
; Use set! to assign a new value to an existing variable
|
||||||
|
(define n 5)
|
||||||
|
(set! n 6)
|
||||||
|
n ; => 6
|
||||||
|
|
||||||
|
; Many Racket datatypes can be immutable or mutable
|
||||||
|
; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...)
|
||||||
|
|
||||||
|
; Use "vector" to create a mutable vector
|
||||||
|
(define vec (vector 2 2 3 4))
|
||||||
|
; Use vector-set! to update a slot
|
||||||
|
(vector-set! vec 0 1)
|
||||||
|
vec ; => #(1 2 3 4)
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; 7. Modules
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Modules let you organize code into multiple files and reusable libraries
|
; Modules let you organize code into multiple files and reusable libraries
|
||||||
@ -322,7 +376,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;(show "~a" 1 #\A) ; => error, "show" was not exported
|
;(show "~a" 1 #\A) ; => error, "show" was not exported
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 6. Classes and Objects
|
;; 8. Classes and Objects
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Create a class fish%
|
; Create a class fish%
|
||||||
@ -330,19 +384,12 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(class object%
|
(class object%
|
||||||
(init size) ; initialization argument
|
(init size) ; initialization argument
|
||||||
(super-new) ; superclass initialization
|
(super-new) ; superclass initialization
|
||||||
|
; Field
|
||||||
(define current-size size) ; field
|
(define current-size size)
|
||||||
|
|
||||||
; Public methods
|
; Public methods
|
||||||
(define/public (get-size)
|
(define/public (get-size) current-size)
|
||||||
current-size)
|
(define/public (grow amt) (set! current-size (+ amt current-size)))
|
||||||
|
(define/public (eat other-fish) (grow (send other-fish get-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%
|
; Create an instance of fish%
|
||||||
(define charlie
|
(define charlie
|
||||||
@ -352,9 +399,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(send charlie grow 6)
|
(send charlie grow 6)
|
||||||
(send charlie get-size) ; => 16
|
(send charlie get-size) ; => 16
|
||||||
|
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 7. Macros
|
;; 9. Macros
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Macros let you extend the syntax of the language
|
; Macros let you extend the syntax of the language
|
||||||
@ -363,7 +409,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
|
|
||||||
(unless (even? 10) "odd" "even") ; => "even"
|
(unless (even? 10) "odd" "even") ; => "even"
|
||||||
|
|
||||||
; Macros are hygienic, there is no risk to clobber existing variables!
|
; Macros are hygienic, you cannot clobber existing variables!
|
||||||
(define-syntax-rule (swap x y)
|
(define-syntax-rule (swap x y)
|
||||||
(begin
|
(begin
|
||||||
(define tmp x)
|
(define tmp x)
|
||||||
@ -374,7 +420,34 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(define a 2)
|
(define a 2)
|
||||||
(define b 3)
|
(define b 3)
|
||||||
(swap a b)
|
(swap a b)
|
||||||
(printf "tmp = ~a; a = ~a; b = ~a" tmp a b) ; tmp is unaffected by swap
|
(printf "tmp = ~a; a = ~a; b = ~a~n" tmp a b) ; tmp is unaffected by swap
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; 10. Contracts
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
; Contracts impose constraints on values exported from modules
|
||||||
|
|
||||||
|
(module bank-account racket
|
||||||
|
(provide (contract-out
|
||||||
|
[deposit (-> positive? any)] ; amount will always be a positive number
|
||||||
|
[balance (-> positive?)]))
|
||||||
|
|
||||||
|
(define amount 0)
|
||||||
|
(define (deposit a) (set! amount (+ amount a)))
|
||||||
|
(define (balance) amount)
|
||||||
|
)
|
||||||
|
|
||||||
|
(require 'bank-account)
|
||||||
|
(deposit 5)
|
||||||
|
|
||||||
|
(balance) ; => 5
|
||||||
|
|
||||||
|
; Any client that attempt to deposit a non-positive amount, will be blamed
|
||||||
|
; (deposit -5) ; => deposit: contract violation
|
||||||
|
; expected: positive?
|
||||||
|
; given: -5
|
||||||
|
; more details....
|
||||||
```
|
```
|
||||||
|
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
Loading…
Reference in New Issue
Block a user