From 340e552822343e755331ba749f5e1081fc094955 Mon Sep 17 00:00:00 2001 From: Manu Date: Thu, 11 Jul 2013 23:45:38 +1200 Subject: started on Racket --- racket.html.markdown | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 racket.html.markdown diff --git a/racket.html.markdown b/racket.html.markdown new file mode 100644 index 00000000..27871e50 --- /dev/null +++ b/racket.html.markdown @@ -0,0 +1,199 @@ +TODO: sequences, control flow, macros, objects, modules + +--- +language: racket +author: Th3rac25 +--- + +Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. + +Feedback would be highly appreciated! You can reach me at [@th3rac25](http://twitter.com/th3rac25) or th3rac25 [at] [google's email service] + + +```racket +; Single line comments start with a semicolon +#| Multiline strings can be written + using three "'s, and are often used + as comments +|# + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 1. Primitive Datatypes and Operators +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; You have numbers +1 +9999999999999999999999 ; big integers +3.14 +6.02e+23 +1/2 ; rationals +1+2i ; complex numbers + +; Math is what you would expect +(+ 1 1) ; => 2 +(- 8 1) ; => 7 +(* 10 2) ; => 20 +(quotient 5 2) ; => 2 +(remainder 5 2) ; => 1 +(/ 35 5) ; => 7 +(/ 1 3) ; => 1/3 +(exact->inexact 1/3) ; => 0.3333333333333333 +(+ 1+2i 2-3i) ; => 3-1i + +; Booleans +#t ; for true +#f ; for false +(not #t) ; => #f +; In conditionals, all non-#f values are treated as true + +; Equality for numbers is = +(= 1 1.0) ; => #t +(= 2 1) ; => #f + +; Characters +#\A ; => #\A +#\λ ; => #\λ +#\u03BB ; => #\λ + +; Strings are fixed-length array of characters. +"Hello, world!" +"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character +"λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant + +; Strings can be added too! +(string-append "Hello " "world!") ; => "Hello world!" + +; A string can be treated like a list of characters +(string-ref "Apple" 0) ; => #\A + +; format can be used to format strings: +(format "~a can be ~a" "strings" "formatted") + +; Printing is pretty easy +(printf "I'm Racket. Nice to meet you!\n") + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 2. Variables and Collections +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; You need to declare variables before assigning to them. +; a variable name can use any characters except: () [] {} " , ' ` ; # | \ +(define some-var 5) +some-var ; => 5 + +; use set! to reassign +(set! some-var 6) +some-var ; => 6 + +; Accessing a previously unassigned variable is an exception +x ; => x: undefined ... + +; if can be used as an expression + +some_var = a if a > b else b +; If a is greater than b, then a is assigned to some_var. +; Otherwise b is assigned to some_var. + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. Collections and Sequences +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; Lists are linked-list data structures, vectors are fixed-length arrays. +'(1 2 3) ; a list +#(1 2 3) ; a vector + +; 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) + +; Use filter, map to interact with collections +(map add1 '(1 2 3)) ; => (2 3 4) +(filter even? '(1 2 3)) ; => (2) + +; Use fold to reduce them +(foldl + 0 '(1 2 3 4)) +; = (+ 1 (+ 2 (+ 3 (+ 4 0))) +; => 10 + +; a sequence is an ordered collection of value +(sequence? '(1 2 3)) ; => #t +(sequence? #(1 2 3)) ; => #t + +;; more sequence stuff here ! + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 3. Functions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +; Use fn to create new functions. A function always returns +; its last statement. +(lambda () "Hello World") ; => # + +; (You need extra parens to call it) +((lambda () "Hello World")) ; => "Hello World" + +; You can create a variable using define +(define x 1) +x ; => 1 + +; Assign a function to a var +(define hello-world (lambda () "Hello World")) +(hello-world) ; => "Hello World" + +; You can shorten this to: +(define (hello-world) "Hello World") + +; 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 +(define hello2 + (case-lambda + [() "Hello World"] + [(name) (string-append "Hello " name)])) +(hello2 "Jake") ; => "Hello Jake" +(hello2) ; => "Hello World" + +; 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 +(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" + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 4. Control Flow +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 5. Modules +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 6. Classes +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; 7. Macros +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +``` + +;; 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 80c2a00bc5502f3afb4c9f50ba8d8197f8dd7700 Mon Sep 17 00:00:00 2001 From: Manu Date: Fri, 12 Jul 2013 00:01:33 +1200 Subject: fix errors --- racket.html.markdown | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 27871e50..db85edb0 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -17,6 +17,9 @@ Feedback would be highly appreciated! You can reach me at [@th3rac25](http://twi as comments |# + +#lang racket ; defines the language we are using + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 1. Primitive Datatypes and Operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -88,12 +91,6 @@ some-var ; => 6 ; Accessing a previously unassigned variable is an exception x ; => x: undefined ... -; if can be used as an expression - -some_var = a if a > b else b -; If a is greater than b, then a is assigned to some_var. -; Otherwise b is assigned to some_var. - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Collections and Sequences ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -144,7 +141,7 @@ x ; => 1 (hello-world) ; => "Hello World" ; You can shorten this to: -(define (hello-world) "Hello World") +(define (hello-world2) "Hello World") ; The () is the list of arguments for the function. (define hello -- cgit v1.2.3 From fb2c8aa765067bd2ff70d6bbabaf5304b849c3f3 Mon Sep 17 00:00:00 2001 From: Manu Date: Fri, 12 Jul 2013 23:33:34 +1200 Subject: added macros, conditionals. fixed some errors --- racket.html.markdown | 130 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 99 insertions(+), 31 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index db85edb0..0d75e8a2 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -1,38 +1,43 @@ -TODO: sequences, control flow, macros, objects, modules - --- language: racket -author: Th3rac25 +author: th3rac25 --- Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family. -Feedback would be highly appreciated! You can reach me at [@th3rac25](http://twitter.com/th3rac25) or th3rac25 [at] [google's email service] +Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3rac25) or th3rac25 [at] [google's email service] ```racket +#lang racket ; defines the language we are using + +; TODO: +; quote +; collections (set, hash) +; structs +; control flow (pattern-matching, loops, sequences) +; objects + + ; Single line comments start with a semicolon #| Multiline strings can be written using three "'s, and are often used as comments |# - -#lang racket ; defines the language we are using - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 1. Primitive Datatypes and Operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; You have numbers -1 9999999999999999999999 ; big integers -3.14 +3.14 ; reals 6.02e+23 -1/2 ; rationals -1+2i ; complex numbers +1/2 ; rationals +1+2i ; complex numbers -; Math is what you would expect +; Operators are functions, functions applications are written +; (f x y z ...) where f is a function and x, y, z, ... are operands (+ 1 1) ; => 2 (- 8 1) ; => 7 (* 10 2) ; => 20 @@ -47,7 +52,6 @@ Feedback would be highly appreciated! You can reach me at [@th3rac25](http://twi #t ; for true #f ; for false (not #t) ; => #f -; In conditionals, all non-#f values are treated as true ; Equality for numbers is = (= 1 1.0) ; => #t @@ -76,23 +80,27 @@ Feedback would be highly appreciated! You can reach me at [@th3rac25](http://twi (printf "I'm Racket. Nice to meet you!\n") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 2. Variables and Collections +;; 2. Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -; You need to declare variables before assigning to them. +; You can create a variable using define ; a variable name can use any characters except: () [] {} " , ' ` ; # | \ (define some-var 5) some-var ; => 5 -; use set! to reassign +; Use set! to assign a new value to an existing variable (set! some-var 6) some-var ; => 6 ; Accessing a previously unassigned variable is an exception -x ; => x: undefined ... +;x ; => x: undefined ... + +; Local binding: me is bound to "Bob" only within (let ...) +(let ([me "Bob"]) + "Alice" + me) ; => "Bob" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 3. Collections and Sequences +;; 3. Collections ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Lists are linked-list data structures, vectors are fixed-length arrays. @@ -114,28 +122,23 @@ x ; => x: undefined ... ; = (+ 1 (+ 2 (+ 3 (+ 4 0))) ; => 10 -; a sequence is an ordered collection of value -(sequence? '(1 2 3)) ; => #t -(sequence? #(1 2 3)) ; => #t +; Set + -;; more sequence stuff here ! +; Hash ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 3. Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Use fn to create new functions. A function always returns +; Use lambda to create new functions. A function always returns ; its last statement. (lambda () "Hello World") ; => # ; (You need extra parens to call it) ((lambda () "Hello World")) ; => "Hello World" -; You can create a variable using define -(define x 1) -x ; => 1 - ; Assign a function to a var (define hello-world (lambda () "Hello World")) (hello-world) ; => "Hello World" @@ -171,12 +174,61 @@ x ; => 1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 4. Control Flow ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; Conditionals +(if #t ; test expression + "this is true" ; then expression + "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 + + +; Cond chains a series of test to select a result +(cond + [(> 2 2) (error "wrong!")] + [(< 2 2) (error "wrong again!")] + [else 'ok]) ; => 'ok +; Pattern matching + +; Loops + +; Sequences + +; 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")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 5. Modules ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - +; 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 + (printf fmt (make-string n ch)) + (newline))) + +(require 'cake) ;; import all 'cake' functions +(print-cake 3) +;(show "~a" 1 #\A) ; => this is an error ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 6. Classes @@ -186,8 +238,24 @@ x ; => 1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 7. Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - - +; Macros let you extend the syntax of the language +(define-syntax-rule (unless test then else) + (if test else then)) + +(unless (even? 10) "odd" "even") ; => "even" + +; Macros are hygienic, there is no risk to clobber existing variables! +(define-syntax-rule (swap x y) + (begin + (define tmp x) + (set! x y) + (set! y tmp))) + +(define tmp 1) +(define a 2) +(define b 3) +(swap a b) +(printf "tmp = ~a; a = ~a; b = ~a" tmp a b) ; tmp is unaffected by swap ``` ;; Further Reading -- cgit v1.2.3 From 930d12e98f92c3ccffc423570e45b479c32c0c51 Mon Sep 17 00:00:00 2001 From: Manu Date: Sun, 14 Jul 2013 00:37:49 +1200 Subject: added sets, hashes and objects --- racket.html.markdown | 108 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 86 insertions(+), 22 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 0d75e8a2..8cc5ac24 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -13,10 +13,8 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ; TODO: ; quote -; collections (set, hash) ; structs ; control flow (pattern-matching, loops, sequences) -; objects ; 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; You have numbers -9999999999999999999999 ; big integers +; Numbers +9999999999999999999999 ; integers 3.14 ; reals 6.02e+23 1/2 ; rationals 1+2i ; complex numbers -; Operators are functions, functions applications are written -; (f x y z ...) where f is a function and x, y, z, ... are operands +; Function application is written (f x y z ...) +; where f is a function and x, y, z, ... are operands + +; Here are a few arithmetic operators (+ 1 1) ; => 2 (- 8 1) ; => 7 (* 10 2) ; => 20 @@ -103,30 +103,65 @@ some-var ; => 6 ;; 3. Collections ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Lists are linked-list data structures, vectors are fixed-length arrays. -'(1 2 3) ; a list -#(1 2 3) ; a vector +; Lists are linked-list data structures +'(1 2 3) + +; Vectors are fixed-length arrays +#(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) -; Use filter, map to interact with collections +; Use "filter", "map" to interact with collections (map add1 '(1 2 3)) ; => (2 3 4) (filter even? '(1 2 3)) ; => (2) -; Use fold to reduce them +; Use "fold" to reduce them (foldl + 0 '(1 2 3 4)) ; = (+ 1 (+ 2 (+ 3 (+ 4 0))) ; => 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) + +; 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)) -; Hash +; 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 @@ -174,6 +209,7 @@ some-var ; => 6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 4. Control Flow ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ; Conditionals (if #t ; test expression "this is true" ; then expression @@ -187,7 +223,7 @@ some-var ; => 6 'nope) ; => 'yep -; Cond chains a series of test to select a result +; "cond" chains a series of tests to select a result (cond [(> 2 2) (error "wrong!")] [(< 2 2) (error "wrong again!")] @@ -200,8 +236,8 @@ some-var ; => 6 ; Sequences ; 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)]) @@ -226,14 +262,42 @@ some-var ; => 6 (printf fmt (make-string n ch)) (newline))) -(require 'cake) ;; import all 'cake' functions +;; Use "require" to import all functions from the module +(require 'cake) (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 @@ -258,7 +322,7 @@ some-var ; => 6 (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/) -- cgit v1.2.3 From 150b232b9b3db24e0f533baa317f06d34293088c Mon Sep 17 00:00:00 2001 From: Manu Date: Sun, 14 Jul 2013 23:59:16 +1200 Subject: added struct, quote --- racket.html.markdown | 111 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 28 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index 8cc5ac24..fed3587c 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -11,12 +11,6 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ```racket #lang racket ; defines the language we are using -; TODO: -; quote -; structs -; control flow (pattern-matching, loops, sequences) - - ; Single line comments start with a semicolon #| Multiline strings can be written using three "'s, and are often used @@ -27,7 +21,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ;; 1. Primitive Datatypes and Operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Numbers +;;; Numbers 9999999999999999999999 ; integers 3.14 ; reals 6.02e+23 @@ -36,8 +30,10 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ; Function application is written (f x y z ...) ; where f is a function and x, y, z, ... are operands - -; Here are a few arithmetic operators +; 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 (+ 1 1) ; => 2 (- 8 1) ; => 7 (* 10 2) ; => 20 @@ -48,7 +44,7 @@ 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 +;;; Booleans #t ; for true #f ; for false (not #t) ; => #f @@ -57,12 +53,12 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r (= 1 1.0) ; => #t (= 2 1) ; => #f -; Characters +;;; Characters #\A ; => #\A #\λ ; => #\λ #\u03BB ; => #\λ -; Strings are fixed-length array of characters. +;;; Strings are fixed-length array of characters. "Hello, world!" "Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character "λx:(μα.α→α).xx" ; any Unicode character can appear in a string constant @@ -83,7 +79,7 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ;; 2. Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; You can create a variable using define -; a variable name can use any characters except: () [] {} " , ' ` ; # | \ +; a variable name can use any character except: ()[]{}",'`;#|\ (define some-var 5) some-var ; => 5 @@ -100,14 +96,29 @@ some-var ; => 6 me) ; => "Bob" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 3. Collections +;; 3. Structs and Collections ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; Structs +(struct dog (name breed age)) +(define my-pet + (dog "lassie" "collie" 5)) +my-pet ; => # +(dog? my-pet) ; => #t +(dog-name my-pet) ; => "lassie" + +; Pairs +; "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 are linked-list data structures -'(1 2 3) +'(1 2 3) ; => '(1 2 3) ; Vectors are fixed-length arrays -#(1 2 3) +#(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) @@ -167,8 +178,8 @@ m ; => '#hash((b . 2) (a . 1) (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) @@ -210,7 +221,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;; 4. Control Flow ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Conditionals +;;; Conditionals + (if #t ; test expression "this is true" ; then expression "this is false" ; else expression @@ -222,20 +234,61 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) '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 -; Pattern matching +;;; Pattern Matching + +(define (fizzbuzz? n) + (match (list (remainder n 3) (remainder n 5)) + [(list 0 0) 'fizzbuzz] + [(list 0 _) 'fizz] + [(list _ 0) 'buzz] + [_ #f])) + +(fizzbuzz? 15) ; => 'fizzbuzz +(fizzbuzz? 37) ; => #f + +;;; Loops + +(define (loop i) + (when (< i 10) + (printf "i:~a~n" i) + (loop (add1 i)))) + +(loop 5) ; => i:5 i:6 ... -; Loops +(let loop ((i 0)) + (when (< i 10) + (printf "i:~a~n" i) + (loop (add1 i)))) ; => i:0 i:1 ... -; Sequences -; Exceptions +;;; Sequences + +; "for" allows iteration over sequences: +; lists, vectors, strings, sets, hash tables, etc... +(for ([i (in-list '(l i s t))]) + (displayln i)) + +(for ([i (in-vector #(v e c t o r))]) + (displayln i)) + +(for ([i (in-string "string")]) + (displayln i)) + +(for ([i (in-set (set 'x 'y 'z))]) + (displayln i)) + +(for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))]) + (printf "key:~a value:~a ~n" k v)) + + +;;; Exceptions + ; To catch an exception, use the "with-handlers" form ; To throw an exception use "raise" (with-handlers @@ -246,11 +299,12 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 5. Modules ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; Modules let you organize code into multiple files and reusable libraries. -(module cake racket/base ;; define a new module 'cake' based on racket/base +; 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 + (provide print-cake) ; function exported by the module (define (print-cake n) (show " ~a " n #\.) @@ -262,7 +316,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (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 @@ -302,6 +356,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 7. Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ; Macros let you extend the syntax of the language (define-syntax-rule (unless test then else) (if test else then)) -- cgit v1.2.3 From 13604e79328b211e5e3bd7f3123be696cd8d3096 Mon Sep 17 00:00:00 2001 From: Manu Date: Mon, 15 Jul 2013 14:44:48 +1200 Subject: corrections and suggestions --- racket.html.markdown | 159 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 116 insertions(+), 43 deletions(-) diff --git a/racket.html.markdown b/racket.html.markdown index fed3587c..707919dd 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -11,12 +11,20 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ```racket #lang racket ; defines the language we are using +;;; Comments + ; Single line comments start with a semicolon -#| Multiline strings can be written - using three "'s, and are often used - as comments + +#| Block 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -46,12 +54,10 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r ;;; Booleans #t ; for true -#f ; for false +#f ; for false -- any value other than #f is true (not #t) ; => #f - -; Equality for numbers is = -(= 1 1.0) ; => #t -(= 2 1) ; => #f +(and 0 #f (error "doesn't get here")) ; => #f +(or #f 0 (error "doesn't get here")) ; => 0 ;;; Characters #\A ; => #\A @@ -83,9 +89,9 @@ Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3r (define some-var 5) some-var ; => 5 -; Use set! to assign a new value to an existing variable -(set! some-var 6) -some-var ; => 6 +; 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 ... @@ -107,18 +113,17 @@ my-pet ; => # (dog? my-pet) ; => #t (dog-name my-pet) ; => "lassie" -; Pairs +;;; Pairs (immutable) ; "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 are linked-list data structures -'(1 2 3) ; => '(1 2 3) +;;; Lists -; Vectors are fixed-length arrays -#(1 2 3) ; => '#(1 2 3) +; 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 (cons 4 '(1 2 3)) ; => (4 1 2 3) @@ -126,14 +131,13 @@ my-pet ; => # ; Use "append" to add lists together (append '(1 2) '(3 4)) ; => (1 2 3 4) -; Use "filter", "map" to interact with collections -(map add1 '(1 2 3)) ; => (2 3 4) -(filter even? '(1 2 3)) ; => (2) +;;; Vectors + +; Vectors are fixed-length arrays +#(1 2 3) ; => '#(1 2 3) -; Use "fold" to reduce them -(foldl + 0 '(1 2 3 4)) -; = (+ 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 @@ -218,7 +222,24 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ; => "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 @@ -254,6 +275,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;;; Loops +; looping can be done through recursion (define (loop i) (when (< i 10) (printf "i:~a~n" i) @@ -261,11 +283,26 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (loop 5) ; => i:5 i:6 ... +; 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 @@ -286,7 +323,6 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (for ([(k v) (in-hash (hash 'a 1 'b 2 'c 3 ))]) (printf "key:~a value:~a ~n" k v)) - ;;; Exceptions ; To catch an exception, use the "with-handlers" form @@ -297,7 +333,25 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (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 @@ -322,7 +376,7 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) ;(show "~a" 1 #\A) ; => error, "show" was not exported ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 6. Classes and Objects +;; 8. Classes and Objects ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Create a class fish% @@ -330,19 +384,12 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (class object% (init size) ; initialization argument (super-new) ; superclass initialization - - (define current-size size) ; field - + ; 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 @@ -352,9 +399,8 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (send charlie grow 6) (send charlie get-size) ; => 16 - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; 7. Macros +;; 9. Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 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" -; 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) (begin (define tmp x) @@ -374,7 +420,34 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) (define a 2) (define b 3) (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 -- cgit v1.2.3