From 4ad1441e839282f97fc3e2031f97a4adc552a707 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:22:11 -0400 Subject: Normalize top comments to start with ";;". Leave a single ";" for commented expressions and output indications. --- racket.html.markdown | 155 +++++++++++++++++++++++++-------------------------- 1 file changed, 77 insertions(+), 78 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 4b72591a..25b92f62 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -17,7 +17,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ;;; Comments -; Single line comments start with a semicolon +;; Single line comments start with a semicolon #| Block comments 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" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -40,12 +40,12 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r 1/2 ; rationals 1+2i ; complex numbers -; Function application is written (f x y z ...) -; 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 -; being evaluated +;; Function application is written (f x y z ...) +;; 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 +;; being evaluated '(+ 1 2) ; => (+ 1 2) -; Now, some arithmetic operations +;; Now, some arithmetic operations (+ 1 1) ; => 2 (- 8 1) ; => 7 (* 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 "λ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!" -; 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 -; format can be used to format strings: +;; format can be used to format strings: (format "~a can be ~a" "strings" "formatted") -; Printing is pretty easy +;; Printing is pretty easy (printf "I'm Racket. Nice to meet you!\n") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 2. Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; You can create a variable using define -; a variable name can use any character except: ()[]{}",'`;#|\ +;; You can create a variable using define +;; a variable name can use any character except: ()[]{}",'`;#|\ (define some-var 5) some-var ; => 5 -; You can also use unicode characters +;; You can also use unicode characters (define ⊆ subset?) (⊆ (set 3 2) (set 1 2 3)); => #t -; Accessing a previously unassigned variable is an exception -;x ; => x: undefined ... +;; Accessing a previously unassigned variable is an exception +; 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"]) "Alice" me) ; => "Bob" @@ -109,7 +109,7 @@ some-var ; => 5 ;; 3. Structs and Collections ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Structs +;; Structs (struct dog (name breed age)) (define my-pet (dog "lassie" "collie" 5)) @@ -118,95 +118,95 @@ my-pet ; => # (dog-name my-pet) ; => "lassie" ;;; Pairs (immutable) -; "cons" constructs pairs, "car" and "cdr" extract the first -; and second elements +;; "cons" constructs pairs, "car" and "cdr" extract the first +;; and second elements (cons 1 2) ; => '(1 . 2) (car (cons 1 2)) ; => 1 (cdr (cons 1 2)) ; => 2 ;;; Lists -; Lists are linked-list data structures +;; Lists are linked-list data structures (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) -; Use "append" to add lists together +;; Use "append" to add lists together (append '(1 2) '(3 4)) ; => (1 2 3 4) ;;; Vectors -; Vectors are fixed-length arrays +;; Vectors are fixed-length arrays #(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) ;;; 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" +;; Add a member with "set-add" (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) -; 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) 4) ; => #f ;;; 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)) -; Retrieve a value +;; Retrieve a value (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 -; You can provide a default value for missing keys +;; 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 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! +;; Remember, these hashes are immutable! 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)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Use lambda to create new functions. -; A function always returns its last statement. +;; Use lambda to create new functions. +;; A function always returns its last statement. (lambda () "Hello World") ; => # -; (You need extra parens to call it) +;; (You need extra parens to call it) ((lambda () "Hello World")) ; => "Hello World" -; Assign a function to a var +;; Assign a function to a var (define hello-world (lambda () "Hello World")) (hello-world) ; => "Hello World" -; You can shorten this to: +;; You can shorten this to: (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 (lambda (name) (string-append "Hello " name))) (hello "Steve") ; => "Hello Steve" -; You can have multi-variadic functions, too +;; You can have multi-variadic functions, too (define hello2 (case-lambda [() "Hello World"] @@ -214,12 +214,12 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (hello2 "Jake") ; => "Hello Jake" (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) (format "You passed ~a args: ~a" (length args) args)) (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) (format "Hello ~a, you passed ~a extra args" name (length args))) (hello-count "Finn" 1 2 3) @@ -229,16 +229,16 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; 4. Equality ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; for numbers use "=" +;; for numbers use "=" (= 3 3.0) ; => #t (= 2 1) ; => #f -; for object identity use "eq?" +;; for object identity use "eq?" (eq? 3 3) ; => #t (eq? 3 3.0) ; => #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 'b 'a)) ; => #f @@ -253,13 +253,13 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) "this is false" ; else expression ) ; => "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") (if (member "Groucho" '("Harpo" "Groucho" "Zeppo")) 'yep 'nope) ; => '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!")] [(< 2 2) (error "wrong again!")] @@ -279,7 +279,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;; Loops -; looping can be done through recursion +;; looping can be done through recursion (define (loop i) (when (< i 10) (printf "i:~a~n" i) @@ -287,7 +287,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (loop 5) ; => i:5 i:6 ... -; similarly, with a named let +;; similarly, with a named let (let loop ((i 0)) (when (< i 10) (printf "i:~a~n" i) @@ -304,14 +304,14 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (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" +;; To combine iteration results, use "for/fold" (for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10 ;;; Sequences -; "for" allows iteration over sequences: -; lists, vectors, strings, sets, hash tables, etc... +;; "for" allows iteration over sequences: +;; lists, vectors, strings, sets, hash tables, etc... (for ([i (in-list '(l i s t))]) (displayln i)) @@ -329,8 +329,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;; Exceptions -; To catch an exception, use the "with-handlers" form -; To throw an exception use "raise" +;; To catch an exception, use the "with-handlers" form +;; To throw an exception use "raise" (with-handlers ([(lambda (v) (equal? v "infinity")) (lambda (exn) +inf.0)]) @@ -340,17 +340,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; 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) (set! n 6) n ; => 6 -; Many Racket datatypes can be immutable or mutable -; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...) +;; Many Racket datatypes can be immutable or mutable +;; (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)) -; Use vector-set! to update a slot +;; Use vector-set! to update a slot (vector-set! vec 0 1) vec ; => #(1 2 3 4) @@ -358,7 +358,7 @@ 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 (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)) (newline))) -; Use "require" to import all functions from the module +;; Use "require" to import all functions from the module (require 'cake) (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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Create a class fish% +;; Create a class fish% (define fish% (class object% (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 (eat other-fish) (grow (send other-fish get-size))))) -; Create an instance of fish% +;; Create an instance of fish% (define charlie (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 get-size) ; => 16 @@ -407,9 +407,9 @@ vec ; => #(1 2 3 4) ;; 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 ...) (let loop () (when condition @@ -421,7 +421,7 @@ vec ; => #(1 2 3 4) (displayln 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) (begin (define tmp x) @@ -438,7 +438,7 @@ vec ; => #(1 2 3 4) ;; 10. Contracts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Contracts impose constraints on values exported from modules +;; Contracts impose constraints on values exported from modules (module bank-account racket (provide (contract-out @@ -455,14 +455,13 @@ vec ; => #(1 2 3 4) (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.... +;; 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 Still up for more? Try [Quick: An Introduction to Racket with Pictures](http://docs.racket-lang.org/quick/) - -- cgit v1.2.3 From fa1ef10eddff3136ebaa38daae2646fdab81ca52 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:22:48 -0400 Subject: Remove spaces at end-of-lines. --- racket.html.markdown | 84 ++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 25b92f62..fa20bf67 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -7,7 +7,7 @@ contributors: --- -Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. +Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3rac25) or th3rac25 [at] [google's email service] @@ -23,12 +23,12 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -37,10 +37,10 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r 9999999999999999999999 ; integers 3.14 ; reals 6.02e+23 -1/2 ; rationals -1+2i ; complex numbers +1/2 ; rationals +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 ;; If you want to create a literal list of data, use ' to stop it from ;; being evaluated @@ -56,16 +56,16 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r (exact->inexact 1/3) ; => 0.3333333333333333 (+ 1+2i 2-3i) ; => 3-1i -;;; Booleans -#t ; for true +;;; Booleans +#t ; for true #f ; for false -- any value other than #f is true (not #t) ; => #f (and 0 #f (error "doesn't get here")) ; => #f (or #f 0 (error "doesn't get here")) ; => 0 -;;; Characters +;;; Characters #\A ; => #\A -#\λ ; => #\λ +#\λ ; => #\λ #\u03BB ; => #\λ ;;; Strings are fixed-length array of characters. @@ -111,7 +111,7 @@ some-var ; => 5 ;; Structs (struct dog (name breed age)) -(define my-pet +(define my-pet (dog "lassie" "collie" 5)) my-pet ; => # (dog? my-pet) ; => #t @@ -173,7 +173,7 @@ my-pet ; => # (hash-ref m 'd 0) ; => 0 ;; 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)) ;; Remember, these hashes are immutable! @@ -186,7 +186,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; 3. Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Use lambda to create new functions. +;; Use lambda to create new functions. ;; A function always returns its last statement. (lambda () "Hello World") ; => # @@ -201,14 +201,14 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (define (hello-world2) "Hello World") ;; The () is the list of arguments for the function. -(define hello +(define hello (lambda (name) (string-append "Hello " name))) (hello "Steve") ; => "Hello Steve" ;; You can have multi-variadic functions, too (define hello2 - (case-lambda + (case-lambda [() "Hello World"] [(name) (string-append "Hello " name)])) (hello2 "Jake") ; => "Hello Jake" @@ -273,9 +273,9 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) [(list 0 _) 'fizz] [(list _ 0) 'buzz] [_ #f])) - + (fizzbuzz? 15) ; => 'fizzbuzz -(fizzbuzz? 37) ; => #f +(fizzbuzz? 37) ; => #f ;;; Loops @@ -283,9 +283,9 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (define (loop i) (when (< i 10) (printf "i:~a~n" i) - (loop (add1 i)))) + (loop (add1 i)))) -(loop 5) ; => i:5 i:6 ... +(loop 5) ; => i:5 i:6 ... ;; similarly, with a named let (let loop ((i 0)) @@ -310,8 +310,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;; Sequences -;; "for" allows iteration over sequences: -;; lists, vectors, strings, sets, hash tables, etc... +;; "for" allows iteration over sequences: +;; lists, vectors, strings, sets, hash tables, etc... (for ([i (in-list '(l i s t))]) (displayln i)) @@ -331,8 +331,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; To catch an exception, use the "with-handlers" form ;; To throw an exception use "raise" -(with-handlers - ([(lambda (v) (equal? v "infinity")) +(with-handlers + ([(lambda (v) (equal? v "infinity")) (lambda (exn) +inf.0)]) (raise "infinity")) @@ -342,10 +342,10 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; Use set! to assign a new value to an existing variable (define n 5) -(set! n 6) +(set! 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...) ;; Use "vector" to create a mutable vector @@ -361,22 +361,22 @@ vec ; => #(1 2 3 4) ;; Modules let you organize code into multiple files and reusable libraries (module cake racket/base ; define a new module 'cake' based on racket/base - + (provide print-cake) ; function exported by the module - + (define (print-cake n) (show " ~a " n #\.) (show " .-~a-. " n #\|) (show " | ~a | " n #\space) (show "---~a---" n #\-)) - - (define (show fmt n ch) ;; internal function + + (define (show fmt n ch) ;; internal function (printf fmt (make-string n ch)) (newline))) ;; Use "require" to import all functions from the module -(require 'cake) -(print-cake 3) +(require 'cake) +(print-cake 3) ; (show "~a" 1 #\A) ; => error, "show" was not exported ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -385,24 +385,24 @@ vec ; => #(1 2 3 4) ;; Create a class fish% (define fish% - (class object% + (class object% (init size) ; initialization argument - (super-new) ; superclass initialization + (super-new) ; superclass initialization ; Field - (define current-size size) + (define current-size size) ; 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 +(define charlie (new fish% [size 10])) ;; Use "send" to call an object's methods (send charlie grow 6) (send charlie get-size) ; => 16 - + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 9. Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -421,14 +421,14 @@ vec ; => #(1 2 3 4) (displayln 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) (begin - (define tmp x) + (define tmp x) (set! x y) (set! y tmp))) -(define tmp 1) +(define tmp 1) (define a 2) (define b 3) (swap a b) @@ -444,7 +444,7 @@ vec ; => #(1 2 3 4) (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) @@ -456,9 +456,9 @@ vec ; => #(1 2 3 4) (balance) ; => 5 ;; 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? -;; given: -5 +;; given: -5 ;; more details.... ``` -- cgit v1.2.3 From 6b282360b029ecc906d587dca8573568785b5cf8 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:28:20 -0400 Subject: Indentation fixes. --- racket.html.markdown | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index fa20bf67..3a1cdd29 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -102,8 +102,8 @@ some-var ; => 5 ;; Local binding: me is bound to "Bob" only within (let ...) (let ([me "Bob"]) - "Alice" - me) ; => "Bob" + "Alice" + me) ; => "Bob" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Structs and Collections @@ -250,20 +250,20 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (if #t ; test expression "this is true" ; then expression - "this is false" ; else expression - ) ; => "this is true" + "this is false") ; else expression +; => "this is true" ;; In conditionals, all non-#f values are treated as true (member "Groucho" '("Harpo" "Groucho" "Zeppo")) ; => '("Groucho" "Zeppo") (if (member "Groucho" '("Harpo" "Groucho" "Zeppo")) - 'yep - 'nope) ; => 'yep + 'yep + 'nope) +; => 'yep ;; "cond" chains a series of tests to select a result -(cond - [(> 2 2) (error "wrong!")] - [(< 2 2) (error "wrong again!")] - [else 'ok]) ; => 'ok +(cond [(> 2 2) (error "wrong!")] + [(< 2 2) (error "wrong again!")] + [else 'ok]) ; => 'ok ;;; Pattern Matching @@ -296,13 +296,13 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;; Comprehensions (for/list ([i '(1 2 3)]) - (add1 i)) ; => '(2 3 4) + (add1 i)) ; => '(2 3 4) (for/list ([i '(1 2 3)] #:when (even? i)) - i) ; => '(2) + i) ; => '(2) (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" (for/fold ([sum 0]) ([i '(1 2 3 4)]) @@ -448,7 +448,7 @@ vec ; => #(1 2 3 4) (define amount 0) (define (deposit a) (set! amount (+ amount a))) (define (balance) amount) -) + ) (require 'bank-account) (deposit 5) -- cgit v1.2.3 From e8f8e2331bf57da87949cd130d6c21dfcb4862ca Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:29:37 -0400 Subject: More comment fixes. --- racket.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 3a1cdd29..e528bc74 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -22,7 +22,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r #| Block comments can span multiple lines and... #| - they can be nested ! + they can be nested! |# |# @@ -370,7 +370,7 @@ vec ; => #(1 2 3 4) (show " | ~a | " n #\space) (show "---~a---" n #\-)) - (define (show fmt n ch) ;; internal function + (define (show fmt n ch) ; internal function (printf fmt (make-string n ch)) (newline))) @@ -388,9 +388,9 @@ vec ; => #(1 2 3 4) (class object% (init size) ; initialization argument (super-new) ; superclass initialization - ; Field + ;; Field (define current-size size) - ; Public methods + ;; 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))))) -- cgit v1.2.3 From d8989c2ae3d685b15c16491e30ae8d40580ed059 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:30:51 -0400 Subject: Improve macro definition. It's more robust to use `let' to have the macro result in its own new scope. (Unrelated to the hygiene point, of course.) --- racket.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index e528bc74..526d008c 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -423,8 +423,7 @@ vec ; => #(1 2 3 4) ;; Macros are hygienic, you cannot clobber existing variables! (define-syntax-rule (swap x y) - (begin - (define tmp x) + (let ([tmp x]) (set! x y) (set! y tmp))) -- cgit v1.2.3 From 3715c5c2b7523cb0f03ed927f9d42644b50ebf11 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:36:52 -0400 Subject: C-escapes example. Also, remove old-style "~n"s. --- racket.html.markdown | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 526d008c..015467d9 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -70,7 +70,8 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ;;; Strings are fixed-length array of characters. "Hello, world!" -"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character +"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character +"Foo\tbar\41\x21\u0021\a\r\n" ; includes C escapes, Unicode "λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant ;; Strings can be added too! @@ -282,7 +283,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; looping can be done through recursion (define (loop i) (when (< i 10) - (printf "i:~a~n" i) + (printf "i:~a\n" i) (loop (add1 i)))) (loop 5) ; => i:5 i:6 ... @@ -290,7 +291,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; similarly, with a named let (let loop ((i 0)) (when (< i 10) - (printf "i:~a~n" i) + (printf "i:~a\n" i) (loop (add1 i)))) ; => i:0 i:1 ... ;;; Comprehensions @@ -325,7 +326,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (displayln i)) (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 @@ -431,7 +432,7 @@ vec ; => #(1 2 3 4) (define a 2) (define b 3) (swap a b) -(printf "tmp = ~a; a = ~a; b = ~a~n" 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 -- cgit v1.2.3 From cfc136dea86c465c6b38ffab41e9b0fe96a3efaf Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:43:42 -0400 Subject: Use idiomatic quoting for identifiers. --- racket.html.markdown | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 015467d9..02c3e228 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -101,7 +101,7 @@ some-var ; => 5 ;; Accessing a previously unassigned variable is an exception ; 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"]) "Alice" me) ; => "Bob" @@ -119,7 +119,7 @@ my-pet ; => # (dog-name my-pet) ; => "lassie" ;;; Pairs (immutable) -;; "cons" constructs pairs, "car" and "cdr" extract the first +;; `cons' constructs pairs, `car' and `cdr' extract the first ;; and second elements (cons 1 2) ; => '(1 . 2) (car (cons 1 2)) ; => 1 @@ -130,10 +130,10 @@ my-pet ; => # ;; Lists are linked-list data structures (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) -;; Use "append" to add lists together +;; Use `append' to add lists together (append '(1 2) '(3 4)) ; => (1 2 3 4) ;;; Vectors @@ -141,7 +141,7 @@ my-pet ; => # ;; Vectors are fixed-length arrays #(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) ;;; Sets @@ -149,13 +149,13 @@ my-pet ; => # ;; 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" +;; Add a member with `set-add' (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) -;; 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) 4) ; => #f @@ -173,14 +173,14 @@ my-pet ; => # ;; 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 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 +;; Use `hash-remove' to remove keys (hash-remove m 'a) ; => '#hash((b . 2) (c . 3)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -230,16 +230,16 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; 4. Equality ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; for numbers use "=" +;; for numbers use `=' (= 3 3.0) ; => #t (= 2 1) ; => #f -;; for object identity use "eq?" +;; for object identity use `eq?' (eq? 3 3) ; => #t (eq? 3 3.0) ; => #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 'b 'a)) ; => #f @@ -261,7 +261,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) 'nope) ; => '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!")] [(< 2 2) (error "wrong again!")] [else 'ok]) ; => 'ok @@ -305,13 +305,13 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (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" +;; To combine iteration results, use `for/fold' (for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10 ;;; Sequences -;; "for" allows iteration over sequences: +;; `for' allows iteration over sequences: ;; lists, vectors, strings, sets, hash tables, etc... (for ([i (in-list '(l i s t))]) (displayln i)) @@ -330,8 +330,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;; Exceptions -;; To catch an exception, use the "with-handlers" form -;; To throw an exception use "raise" +;; To catch an exception, use the `with-handlers' form +;; To throw an exception use `raise' (with-handlers ([(lambda (v) (equal? v "infinity")) (lambda (exn) +inf.0)]) @@ -349,7 +349,7 @@ n ; => 6 ;; Many Racket datatypes can be immutable or mutable ;; (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)) ;; Use vector-set! to update a slot (vector-set! vec 0 1) @@ -375,10 +375,10 @@ vec ; => #(1 2 3 4) (printf fmt (make-string n ch)) (newline))) -;; Use "require" to import all functions from the module +;; Use `require' to import all functions from the module (require 'cake) (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 @@ -400,7 +400,7 @@ vec ; => #(1 2 3 4) (define charlie (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 get-size) ; => 16 -- cgit v1.2.3 From 5d952fb6dab4989454c7e2e43fac664180817df8 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:46:06 -0400 Subject: Improve #; example. Also, I removed the following expression since this is the beginning when interaction is not shown. Instead, I added a comment to explain what #;s are useful for. --- racket.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 02c3e228..21769e90 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -26,8 +26,9 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r |# |# -;; S-expression comments discard the following expression -#; "this expression will be discarded" "2nd expression" ; => "2nd expression" +;; S-expression comments discard the following expression, +;; useful to comment expressions when debugging +#; (this expression is discarded) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 1. Primitive Datatypes and Operators -- cgit v1.2.3 From 6af5c957d039eb4712092dcaf1e531ae4e9a35f9 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 01:55:29 -0400 Subject: Clarify functional data structures. Note that `set-add' is functional, clarify immutable hashes, add mutable hash examples to contrast. --- racket.html.markdown | 25 ++++++++++++++++++------- 1 file 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 ; => # ;;; 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 ; => # ;;; 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 ; => # ;; 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3 From e39faf701938f2b0efd52d51cb69435126c35bf5 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 02:03:51 -0400 Subject: Width. Make the whole text fit in 70 characters. --- racket.html.markdown | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 7ae9dad0..19b7326d 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -73,7 +73,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r "Hello, world!" "Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character "Foo\tbar\41\x21\u0021\a\r\n" ; includes C escapes, Unicode -"λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant +"λx:(μα.α→α).xx" ; can include Unicode characters ;; Strings can be added too! (string-append "Hello " "world!") ; => "Hello world!" @@ -267,8 +267,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ; => "this is true" ;; In conditionals, all non-#f values are treated as true -(member "Groucho" '("Harpo" "Groucho" "Zeppo")) ; => '("Groucho" "Zeppo") -(if (member "Groucho" '("Harpo" "Groucho" "Zeppo")) +(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo) +(if (member 'Groucho '(Harpo Groucho Zeppo)) 'yep 'nope) ; => 'yep @@ -315,7 +315,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' i) ; => '(2) (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' (for/fold ([sum 0]) ([i '(1 2 3 4)]) @@ -371,9 +372,11 @@ 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; here we use sub-modules, nested in the whole module that +;; this text makes (starting from the "#lang" line) -(module cake racket/base ; define a new module 'cake' based on racket/base +(module cake racket/base ; define a `cake' module based on racket/base (provide print-cake) ; function exported by the module @@ -404,9 +407,12 @@ vec ; => #(1 2 3 4) ;; Field (define current-size size) ;; 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))))) + (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 @@ -444,7 +450,7 @@ vec ; => #(1 2 3 4) (define a 2) (define b 3) (swap a b) -(printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected by swap +(printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 10. Contracts @@ -454,7 +460,7 @@ vec ; => #(1 2 3 4) (module bank-account racket (provide (contract-out - [deposit (-> positive? any)] ; amount will always be a positive number + [deposit (-> positive? any)] ; amounts are always positive [balance (-> positive?)])) (define amount 0) @@ -467,7 +473,7 @@ vec ; => #(1 2 3 4) (balance) ; => 5 -;; Any client that attempt to deposit a non-positive amount, will be blamed +;; Clients that attempt to deposit a non-positive amount are blamed ;; (deposit -5) ; => deposit: contract violation ;; expected: positive? ;; given: -5 -- cgit v1.2.3 From fbd215521f9b0439fc218957ee72f041bfde933b Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 02:04:56 -0400 Subject: Better link for more reading. --- racket.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/racket.html.markdown b/racket.html.markdown index 19b7326d..bf94db90 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -482,4 +482,4 @@ vec ; => #(1 2 3 4) ## 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 [Getting Started with Racket](http://docs.racket-lang.org/getting-started/) -- cgit v1.2.3 From 7a92ee7ab8833ba3871387613ce14eb958aee11a Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 02:06:16 -0400 Subject: `swap' -> `swap!' --- racket.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index bf94db90..d43ac5e2 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -441,7 +441,7 @@ vec ; => #(1 2 3 4) (set! i (add1 i)))) ;; Macros are hygienic, you cannot clobber existing variables! -(define-syntax-rule (swap x y) +(define-syntax-rule (swap! x y) ; -! is idomatic for mutation (let ([tmp x]) (set! x y) (set! y tmp))) @@ -449,7 +449,7 @@ vec ; => #(1 2 3 4) (define tmp 1) (define a 2) (define b 3) -(swap a b) +(swap! a b) (printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3 From 159b7e4e1e845304bd8eb4398677010b86c7f249 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 02:08:28 -0400 Subject: Add boxes example. --- racket.html.markdown | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index d43ac5e2..fbd3f76a 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -354,11 +354,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;; 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) -(set! n 6) +(set! n (add1 n)) n ; => 6 +;; Use boxes for explicitly mutable values (similar to pointers or +;; references in other languages) +(define n* (box 5)) +(set-box! n* (add1 (unbox n*))) +(unbox n*) ; => 6 + ;; Many Racket datatypes can be immutable or mutable ;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...) -- cgit v1.2.3 From 547a8a6db1fb74a843bb0fbf1f81dd3d67eb92c4 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:06:11 -0400 Subject: Improve mutable/immutable comment, add `make-vector' example. --- racket.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index fbd3f76a..90b29d21 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -365,13 +365,16 @@ n ; => 6 (set-box! n* (add1 (unbox n*))) (unbox n*) ; => 6 -;; Many Racket datatypes can be immutable or mutable -;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...) +;; Many Racket datatypes are immutable (pairs, lists, etc), some come in +;; both mutable and immutable flavors (strings, vectors, hash tables, +;; etc...) -;; Use `vector' to create a mutable vector +;; Use `vector' or `make-vector' to create mutable vectors (define vec (vector 2 2 3 4)) +(define wall (make-vector 100 'bottle-of-beer)) ;; Use vector-set! to update a slot (vector-set! vec 0 1) +(vector-set! wall 99 'down) vec ; => #(1 2 3 4) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3 From d8e722d6193ef17d1420bbeb934219dcde31c6bf Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:12:27 -0400 Subject: Improve list section. --- racket.html.markdown | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 90b29d21..dfd30438 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -128,14 +128,19 @@ my-pet ; => # ;;; Lists -;; Lists are linked-list data structures +;; Lists are linked-list data structures, made of `cons' pairs and end +;; with a `null' (or '()) to mark the end of the list +(cons 1 (cons 2 (cons 3 null))) ; => '(1 2 3) +;; `list' is a convenience variadic constructor for lists (list 1 2 3) ; => '(1 2 3) +;; and a quote can also be used for a literal list value +'(1 2 3) ; => '(1 2 3) -;; Use `cons' to add an item to the beginning of a list -(cons 4 '(1 2 3)) ; => (4 1 2 3) +;; Can still use `cons' to add an item to the beginning of a list +(cons 4 '(1 2 3)) ; => '(4 1 2 3) ;; 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 -- cgit v1.2.3 From 8a065aa2562f55acf3f676b14d208ebbf86e7649 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:20:21 -0400 Subject: Improve functions intro. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `λ', sugared/unsugared examples, optionals, keywords. --- racket.html.markdown | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index dfd30438..d3403ff8 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -204,44 +204,71 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;; 3. Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Use lambda to create new functions. -;; A function always returns its last statement. +;; Use `lambda' to create functions. +;; A function always returns the value of its last expression (lambda () "Hello World") ; => # +;; Can also use a unicode `λ' +(λ () "Hellow World") ; => same function -;; (You need extra parens to call it) +;; Use parens to call all functions, including a lambda expression ((lambda () "Hello World")) ; => "Hello World" +((λ () "Hello World")) ; => "Hello World" ;; Assign a function to a var (define hello-world (lambda () "Hello World")) (hello-world) ; => "Hello World" -;; You can shorten this to: +;; You can shorten this using the function definition syntatcic sugae: (define (hello-world2) "Hello World") -;; The () is the list of arguments for the function. +;; The () in the above is the list of arguments for the function (define hello (lambda (name) (string-append "Hello " name))) (hello "Steve") ; => "Hello Steve" +;; ... or equivalently, using a sugared definition: +(define (hello2 name) + (string-append "Hello " name)) -;; You can have multi-variadic functions, too -(define hello2 +;; You can have multi-variadic functions too, using `case-lambda' +(define hello3 (case-lambda [() "Hello World"] [(name) (string-append "Hello " name)])) -(hello2 "Jake") ; => "Hello Jake" -(hello2) ; => "Hello World" +(hello3 "Jake") ; => "Hello Jake" +(hello3) ; => "Hello World" +;; ... or specify optional arguments with a default value expression +(define (hello4 [name "World"]) + (string-append "Hello " name)) ;; Functions can pack extra arguments up in a list (define (count-args . args) (format "You passed ~a args: ~a" (length args) args)) (count-args 1 2 3) ; => "You passed 3 args: (1 2 3)" +;; ... or with the unsugared `lambda' form: +(define count-args2 + (lambda args + (format "You passed ~a args: ~a" (length args) args))) ;; You can mix regular and packed arguments (define (hello-count name . args) (format "Hello ~a, you passed ~a extra args" name (length args))) (hello-count "Finn" 1 2 3) ; => "Hello Finn, you passed 3 extra args" +;; ... unsugared: +(define hello-count2 + (lambda (name . args) + (format "Hello ~a, you passed ~a extra args" name (length args)))) + +;; And with keywords +(define (hello-k #:name [name "World"] #:greeting [g "Hello"] . args) + (format "~a ~a, ~a extra args" g name (length args))) +(hello-k) ; => "Hello World, 0 extra args" +(hello-k 1 2 3) ; => "Hello World, 3 extra args" +(hello-k #:greeting "Hi") ; => "Hi World, 0 extra args" +(hello-k #:name "Finn" #:greeting "Hey") ; => "Hey Finn, 0 extra args" +(hello-k 1 2 3 #:greeting "Hi" #:name "Finn" 4 5 6) + ; => "Hi Finn, 6 extra args" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 4. Equality -- cgit v1.2.3 From c11ec1f137eb4498818516ab408edb3cfae8317e Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:32:03 -0400 Subject: Improve `with-handlers' an exceptions description. --- racket.html.markdown | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index d3403ff8..4b34fbef 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -375,12 +375,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;;; Exceptions -;; To catch an exception, use the `with-handlers' form -;; To throw an exception use `raise' -(with-handlers - ([(lambda (v) (equal? v "infinity")) - (lambda (exn) +inf.0)]) - (raise "infinity")) +;; To catch exceptions, use the `with-handlers' form +(with-handlers ([exn:fail? (lambda (exn) 999)]) + (+ 1 "2")) ; => 999 +(with-handlers ([exn:break? (lambda (exn) "no time")]) + (sleep 3) + "phew") ; => "phew", but if you break it => "no time" + +;; Use `raise' to throw exceptions or any other value +(with-handlers ([number? ; catch numeric values raised + identity]) ; return them as plain values + (+ 1 (raise 2))) ; => 2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 6. Mutation -- cgit v1.2.3 From 8f295f4e6893620181270a35a22d19926d98f626 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:36:54 -0400 Subject: `require' gets all bindings, not just functions. Also, explain the ' use. --- racket.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 4b34fbef..ab7d3a69 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -436,8 +436,8 @@ vec ; => #(1 2 3 4) (printf fmt (make-string n ch)) (newline))) -;; Use `require' to import all functions from the module -(require 'cake) +;; Use `require' to get all `provide'd names from a module +(require 'cake) ; the ' is for a local submodule (print-cake 3) ; (show "~a" 1 #\A) ; => error, `show' was not exported -- cgit v1.2.3 From c99ac1be7ea81d275564b4a4ae573a45f017e69d Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:44:26 -0400 Subject: Improve class section. --- racket.html.markdown | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/racket.html.markdown b/racket.html.markdown index ab7d3a69..006d6b86 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -445,7 +445,7 @@ vec ; => #(1 2 3 4) ;; 8. Classes and Objects ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Create a class fish% +;; Create a class fish% (-% is idomatic for class bindings) (define fish% (class object% (init size) ; initialization argument @@ -465,9 +465,23 @@ vec ; => #(1 2 3 4) (new fish% [size 10])) ;; Use `send' to call an object's methods +(send charlie get-size) ; => 10 (send charlie grow 6) (send charlie get-size) ; => 16 +;; `fish%' is a plain "first class" value, which can get us mixins +(define (add-color c%) + (class c% + (init color) + (super-new) + (define my-color color) + (define/public (get-color) my-color))) +(define colored-fish% (add-color fish%)) +(define charlie2 (new colored-fish% [size 10] [color 'red])) +(send charlie2 get-color) +;; or, with no names: +(send (new (add-color fish%) [size 10] [color 'red]) get-color) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 9. Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3 From b55ce4f045d62aa9224827aed6133827cc698788 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 04:06:10 -0400 Subject: Much more on iteration in all forms. --- racket.html.markdown | 90 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 006d6b86..4b5465d9 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -324,40 +324,30 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' ;;; Loops -;; looping can be done through recursion +;; Looping can be done through (tail-) recursion (define (loop i) (when (< i 10) - (printf "i:~a\n" i) + (printf "i=~a\n" i) (loop (add1 i)))) +(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)) (when (< i 10) - (printf "i:~a\n" i) - (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 - -;; `for' allows iteration over sequences: + (printf "i=~a\n" i) + (loop (add1 i)))) ; => i=0, i=1, ... + +;; See below how to add a new `loop' form, but Racket already has a very +;; flexible `for' form for loops: +(for ([i 10]) + (printf "i=~a\n" i)) ; => i=0, i=1, ... +(for ([i (in-range 5 10)]) + (printf "i=~a\n" i)) ; => i=5, i=6, ... + +;;; Other Sequences +;; `for' allows iteration over many other kinds of sequences: ;; lists, vectors, strings, sets, hash tables, etc... + (for ([i (in-list '(l i s t))]) (displayln i)) @@ -373,6 +363,52 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' (for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))]) (printf "key:~a value:~a\n" k v)) +;;; More Complex Iterations + +;; Parallel scan of multiple sequences (stops on shortest) +(for ([i 10] [j '(x y z)]) (printf "~a:~a\n" i j)) +; => 0:x 1:y 2:z + +;; Nested loops +(for* ([i 2] [j '(x y z)]) (printf "~a:~a\n" i j)) +; => 0:x, 0:y, 0:z, 1:x, 1:y, 1:z + +;; Conditions +(for ([i 1000] + #:when (> i 5) + #:unless (odd? i) + #:break (> i 10)) + (printf "i=~a\n" i)) +; => i=6, i=8, i=10 + +;;; Comprehensions +;; Very similar to `for' loops -- just collect the results + +(for/list ([i '(1 2 3)]) + (add1 i)) ; => '(2 3 4) + +(for/list ([i '(1 2 3)] #:when (even? i)) + i) ; => '(2) + +(for/list ([i 10] [j '(x y z)]) + (list i j)) ; => '((0 x) (1 y) (2 z)) + +(for/list ([i 1000] #:when (> i 5) #:unless (odd? i) #:break (> i 10)) + i) ; => '(6 8 10) + +(for/hash ([i '(1 2 3)]) + (values i (number->string i))) +; => '#hash((1 . "1") (2 . "2") (3 . "3")) + +;; There are many kinds of other built-in ways to collect loop values: +(for/sum ([i 10]) (* i i)) ; => 285 +(for/product ([i (in-range 1 11)]) (* i i)) ; => 13168189440000 +(for/and ([i 10] [j (in-range 10 20)]) (< i j)) ; => #t +(for/or ([i 10] [j (in-range 0 20 2)]) (= i j)) ; => #t +;; And to use any arbitrary combination, use `for/fold' +(for/fold ([sum 0]) ([i '(1 2 3 4)]) (+ sum i)) ; => 10 +;; (This can often replace common imperative loops) + ;;; Exceptions ;; To catch exceptions, use the `with-handlers' form -- cgit v1.2.3 From 0063574a4d574e4c5fe85c65159f760be7009da2 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 04:13:06 -0400 Subject: Some list function examples. --- racket.html.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/racket.html.markdown b/racket.html.markdown index 4b5465d9..eb2bf4c0 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -142,6 +142,15 @@ my-pet ; => # ;; Use `append' to add lists together (append '(1 2) '(3 4)) ; => '(1 2 3 4) +;; Lists are a very basic type, so there is a *lot* of functionality for +;; them, a few examples: +(map add1 '(1 2 3)) ; => '(2 3 4) +(map + '(1 2 3) '(10 20 30)) ; => '(11 22 33) +(filter even? '(1 2 3 4)) ; => '(2 4) +(count even? '(1 2 3 4)) ; => 2 +(take '(1 2 3 4) 2) ; => '(1 2) +(drop '(1 2 3 4) 2) ; => '(3 4) + ;;; Vectors ;; Vectors are fixed-length arrays -- cgit v1.2.3 From d97565efa1c220790716a14e7eb74365c358bf24 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 04:17:43 -0400 Subject: Quick bad-macro example. --- racket.html.markdown | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/racket.html.markdown b/racket.html.markdown index eb2bf4c0..e894552c 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -353,7 +353,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d' (for ([i (in-range 5 10)]) (printf "i=~a\n" i)) ; => i=5, i=6, ... -;;; Other Sequences +;;; Iteration Over Other Sequences ;; `for' allows iteration over many other kinds of sequences: ;; lists, vectors, strings, sets, hash tables, etc... @@ -557,6 +557,14 @@ vec ; => #(1 2 3 4) (swap! a b) (printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected +;; But the are still code transformations, for example: +(define-syntax-rule (bad-while condition body ...) + (when condition + body ... + (bad-while condition body ...))) +;; this macro is broken: it generates infinite code, if you try to use +;; it, the compiler will get in an infinite loop + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 10. Contracts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3