mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Normalize top comments to start with ";;".
Leave a single ";" for commented expressions and output indications.
This commit is contained in:
parent
4718ea4e0c
commit
4ad1441e83
@ -17,7 +17,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
|
|
||||||
;;; Comments
|
;;; Comments
|
||||||
|
|
||||||
; Single line comments start with a semicolon
|
;; Single line comments start with a semicolon
|
||||||
|
|
||||||
#| Block comments
|
#| Block comments
|
||||||
can span multiple lines and...
|
can span multiple lines and...
|
||||||
@ -26,7 +26,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
|#
|
|#
|
||||||
|#
|
|#
|
||||||
|
|
||||||
; S-expression comments discard the following expression
|
;; S-expression comments discard the following expression
|
||||||
#; "this expression will be discarded" "2nd expression" ; => "2nd expression"
|
#; "this expression will be discarded" "2nd expression" ; => "2nd expression"
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
@ -40,12 +40,12 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
1/2 ; rationals
|
1/2 ; rationals
|
||||||
1+2i ; complex numbers
|
1+2i ; complex numbers
|
||||||
|
|
||||||
; Function application is written (f x y z ...)
|
;; Function application is written (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
|
||||||
; If you want to create a literal list of data, use ' to stop it from
|
;; If you want to create a literal list of data, use ' to stop it from
|
||||||
; being evaluated
|
;; being evaluated
|
||||||
'(+ 1 2) ; => (+ 1 2)
|
'(+ 1 2) ; => (+ 1 2)
|
||||||
; Now, some arithmetic operations
|
;; Now, some arithmetic operations
|
||||||
(+ 1 1) ; => 2
|
(+ 1 1) ; => 2
|
||||||
(- 8 1) ; => 7
|
(- 8 1) ; => 7
|
||||||
(* 10 2) ; => 20
|
(* 10 2) ; => 20
|
||||||
@ -73,34 +73,34 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r
|
|||||||
"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character
|
"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character
|
||||||
"λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant
|
"λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant
|
||||||
|
|
||||||
; Strings can be added too!
|
;; Strings can be added too!
|
||||||
(string-append "Hello " "world!") ; => "Hello world!"
|
(string-append "Hello " "world!") ; => "Hello world!"
|
||||||
|
|
||||||
; A string can be treated like a list of characters
|
;; A string can be treated like a list of characters
|
||||||
(string-ref "Apple" 0) ; => #\A
|
(string-ref "Apple" 0) ; => #\A
|
||||||
|
|
||||||
; format can be used to format strings:
|
;; format can be used to format strings:
|
||||||
(format "~a can be ~a" "strings" "formatted")
|
(format "~a can be ~a" "strings" "formatted")
|
||||||
|
|
||||||
; Printing is pretty easy
|
;; Printing is pretty easy
|
||||||
(printf "I'm Racket. Nice to meet you!\n")
|
(printf "I'm Racket. Nice to meet you!\n")
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 2. Variables
|
;; 2. Variables
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
; You can create a variable using define
|
;; You can create a variable using define
|
||||||
; a variable name can use any character except: ()[]{}",'`;#|\
|
;; a variable name can use any character except: ()[]{}",'`;#|\
|
||||||
(define some-var 5)
|
(define some-var 5)
|
||||||
some-var ; => 5
|
some-var ; => 5
|
||||||
|
|
||||||
; You can also use unicode characters
|
;; You can also use unicode characters
|
||||||
(define ⊆ subset?)
|
(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
|
;; 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 (let ...)
|
||||||
(let ([me "Bob"])
|
(let ([me "Bob"])
|
||||||
"Alice"
|
"Alice"
|
||||||
me) ; => "Bob"
|
me) ; => "Bob"
|
||||||
@ -109,7 +109,7 @@ some-var ; => 5
|
|||||||
;; 3. Structs and Collections
|
;; 3. Structs and Collections
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Structs
|
;; Structs
|
||||||
(struct dog (name breed age))
|
(struct dog (name breed age))
|
||||||
(define my-pet
|
(define my-pet
|
||||||
(dog "lassie" "collie" 5))
|
(dog "lassie" "collie" 5))
|
||||||
@ -118,95 +118,95 @@ 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
|
||||||
(cdr (cons 1 2)) ; => 2
|
(cdr (cons 1 2)) ; => 2
|
||||||
|
|
||||||
;;; Lists
|
;;; Lists
|
||||||
|
|
||||||
; 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
|
||||||
|
|
||||||
; 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
|
||||||
|
|
||||||
; 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
|
||||||
|
|
||||||
;;; Hashes
|
;;; Hashes
|
||||||
|
|
||||||
; Create an immutable hash table (There are also mutables ones)
|
;; Create an immutable hash table (There are also mutables ones)
|
||||||
(define m (hash 'a 1 'b 2 'c 3))
|
(define m (hash 'a 1 'b 2 'c 3))
|
||||||
|
|
||||||
; Retrieve a value
|
;; Retrieve a value
|
||||||
(hash-ref m 'a) ; => 1
|
(hash-ref m 'a) ; => 1
|
||||||
|
|
||||||
; Retrieving a non-present value is an exception
|
;; Retrieving a non-present value is an exception
|
||||||
; (hash-ref m 'd) => no value found
|
; (hash-ref m 'd) => no value found
|
||||||
|
|
||||||
; 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))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 3. Functions
|
;; 3. Functions
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Use lambda to create new functions.
|
;; Use lambda to create new functions.
|
||||||
; A function always returns its last statement.
|
;; A function always returns its last statement.
|
||||||
(lambda () "Hello World") ; => #<procedure>
|
(lambda () "Hello World") ; => #<procedure>
|
||||||
|
|
||||||
; (You need extra parens to call it)
|
;; (You need extra parens to call it)
|
||||||
((lambda () "Hello World")) ; => "Hello World"
|
((lambda () "Hello World")) ; => "Hello World"
|
||||||
|
|
||||||
; Assign a function to a var
|
;; Assign a function to a var
|
||||||
(define hello-world (lambda () "Hello World"))
|
(define hello-world (lambda () "Hello World"))
|
||||||
(hello-world) ; => "Hello World"
|
(hello-world) ; => "Hello World"
|
||||||
|
|
||||||
; You can shorten this to:
|
;; You can shorten this to:
|
||||||
(define (hello-world2) "Hello World")
|
(define (hello-world2) "Hello World")
|
||||||
|
|
||||||
; The () is the list of arguments for the function.
|
;; The () is the list of arguments for the function.
|
||||||
(define hello
|
(define hello
|
||||||
(lambda (name)
|
(lambda (name)
|
||||||
(string-append "Hello " name)))
|
(string-append "Hello " name)))
|
||||||
(hello "Steve") ; => "Hello Steve"
|
(hello "Steve") ; => "Hello Steve"
|
||||||
|
|
||||||
; You can have multi-variadic functions, too
|
;; You can have multi-variadic functions, too
|
||||||
(define hello2
|
(define hello2
|
||||||
(case-lambda
|
(case-lambda
|
||||||
[() "Hello World"]
|
[() "Hello World"]
|
||||||
@ -214,12 +214,12 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
(hello2 "Jake") ; => "Hello Jake"
|
(hello2 "Jake") ; => "Hello Jake"
|
||||||
(hello2) ; => "Hello World"
|
(hello2) ; => "Hello World"
|
||||||
|
|
||||||
; Functions can pack extra arguments up in a list
|
;; Functions can pack extra arguments up in a list
|
||||||
(define (count-args . args)
|
(define (count-args . args)
|
||||||
(format "You passed ~a args: ~a" (length args) args))
|
(format "You passed ~a args: ~a" (length args) args))
|
||||||
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
|
(count-args 1 2 3) ; => "You passed 3 args: (1 2 3)"
|
||||||
|
|
||||||
; You can mix regular and packed arguments
|
;; You can mix regular and packed arguments
|
||||||
(define (hello-count name . args)
|
(define (hello-count name . args)
|
||||||
(format "Hello ~a, you passed ~a extra args" name (length args)))
|
(format "Hello ~a, you passed ~a extra args" name (length args)))
|
||||||
(hello-count "Finn" 1 2 3)
|
(hello-count "Finn" 1 2 3)
|
||||||
@ -229,16 +229,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
|
||||||
|
|
||||||
@ -253,13 +253,13 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
"this is false" ; else expression
|
"this is false" ; else expression
|
||||||
) ; => "this is true"
|
) ; => "this is true"
|
||||||
|
|
||||||
; In conditionals, all non-#f values are treated as true
|
;; In conditionals, all non-#f values are treated as true
|
||||||
(member "Groucho" '("Harpo" "Groucho" "Zeppo")) ; => '("Groucho" "Zeppo")
|
(member "Groucho" '("Harpo" "Groucho" "Zeppo")) ; => '("Groucho" "Zeppo")
|
||||||
(if (member "Groucho" '("Harpo" "Groucho" "Zeppo"))
|
(if (member "Groucho" '("Harpo" "Groucho" "Zeppo"))
|
||||||
'yep
|
'yep
|
||||||
'nope) ; => 'yep
|
'nope) ; => 'yep
|
||||||
|
|
||||||
; "cond" chains a series of tests 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!")]
|
||||||
@ -279,7 +279,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
|
|
||||||
;;; Loops
|
;;; Loops
|
||||||
|
|
||||||
; looping can be done through recursion
|
;; 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)
|
||||||
@ -287,7 +287,7 @@ 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
|
;; 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)
|
||||||
@ -304,14 +304,14 @@ 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))
|
||||||
|
|
||||||
@ -329,8 +329,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)])
|
||||||
@ -340,17 +340,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3))
|
|||||||
;; 6. Mutation
|
;; 6. Mutation
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Use set! to assign a new value to an existing variable
|
;; Use set! to assign a new value to an existing variable
|
||||||
(define n 5)
|
(define n 5)
|
||||||
(set! n 6)
|
(set! n 6)
|
||||||
n ; => 6
|
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)
|
||||||
vec ; => #(1 2 3 4)
|
vec ; => #(1 2 3 4)
|
||||||
|
|
||||||
@ -358,7 +358,7 @@ vec ; => #(1 2 3 4)
|
|||||||
;; 7. Modules
|
;; 7. Modules
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Modules let you organize code into multiple files and reusable libraries
|
;; Modules let you organize code into multiple files and reusable libraries
|
||||||
|
|
||||||
(module cake racket/base ; define a new module 'cake' based on racket/base
|
(module cake racket/base ; define a new module 'cake' based on racket/base
|
||||||
|
|
||||||
@ -374,16 +374,16 @@ 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
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Create a class fish%
|
;; Create a class fish%
|
||||||
(define fish%
|
(define fish%
|
||||||
(class object%
|
(class object%
|
||||||
(init size) ; initialization argument
|
(init size) ; initialization argument
|
||||||
@ -395,11 +395,11 @@ vec ; => #(1 2 3 4)
|
|||||||
(define/public (grow amt) (set! current-size (+ amt 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 (eat other-fish) (grow (send other-fish get-size)))))
|
||||||
|
|
||||||
; Create an instance of fish%
|
;; Create an instance of fish%
|
||||||
(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
|
||||||
|
|
||||||
@ -407,9 +407,9 @@ vec ; => #(1 2 3 4)
|
|||||||
;; 9. Macros
|
;; 9. Macros
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Macros let you extend the syntax of the language
|
;; Macros let you extend the syntax of the language
|
||||||
|
|
||||||
; Let's add a while loop
|
;; Let's add a while loop
|
||||||
(define-syntax-rule (while condition body ...)
|
(define-syntax-rule (while condition body ...)
|
||||||
(let loop ()
|
(let loop ()
|
||||||
(when condition
|
(when condition
|
||||||
@ -421,7 +421,7 @@ vec ; => #(1 2 3 4)
|
|||||||
(displayln i)
|
(displayln i)
|
||||||
(set! i (add1 i))))
|
(set! i (add1 i))))
|
||||||
|
|
||||||
; Macros are hygienic, you cannot 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)
|
||||||
@ -438,7 +438,7 @@ vec ; => #(1 2 3 4)
|
|||||||
;; 10. Contracts
|
;; 10. Contracts
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
; Contracts impose constraints on values exported from modules
|
;; Contracts impose constraints on values exported from modules
|
||||||
|
|
||||||
(module bank-account racket
|
(module bank-account racket
|
||||||
(provide (contract-out
|
(provide (contract-out
|
||||||
@ -455,14 +455,13 @@ vec ; => #(1 2 3 4)
|
|||||||
|
|
||||||
(balance) ; => 5
|
(balance) ; => 5
|
||||||
|
|
||||||
; Any client that attempt to deposit a non-positive amount, will be blamed
|
;; Any client that attempt to deposit a non-positive amount, will be blamed
|
||||||
; (deposit -5) ; => deposit: contract violation
|
;; (deposit -5) ; => deposit: contract violation
|
||||||
; expected: positive?
|
;; expected: positive?
|
||||||
; given: -5
|
;; given: -5
|
||||||
; more details....
|
;; more details....
|
||||||
```
|
```
|
||||||
|
|
||||||
## 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/)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user