2013-07-11 11:45:38 +00:00
|
|
|
---
|
2013-07-16 01:41:26 +00:00
|
|
|
|
2013-07-11 11:45:38 +00:00
|
|
|
language: racket
|
2013-07-16 01:41:26 +00:00
|
|
|
filename: learnracket.rkt
|
|
|
|
contributors:
|
|
|
|
- ["th3rac25", "https://github.com/voila"]
|
|
|
|
|
2013-07-11 11:45:38 +00:00
|
|
|
---
|
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
Racket is a general purpose, multi-paradigm programming language in the Lisp/Scheme family.
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-12 11:33:34 +00:00
|
|
|
Feedback is appreciated! You can reach me at [@th3rac25](http://twitter.com/th3rac25) or th3rac25 [at] [google's email service]
|
2013-07-11 11:45:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
```racket
|
2013-07-12 11:33:34 +00:00
|
|
|
#lang racket ; defines the language we are using
|
|
|
|
|
2013-07-15 02:44:48 +00:00
|
|
|
;;; Comments
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Single line comments start with a semicolon
|
2013-07-15 02:44:48 +00:00
|
|
|
|
|
|
|
#| Block comments
|
|
|
|
can span multiple lines and...
|
|
|
|
#|
|
2013-07-16 05:29:37 +00:00
|
|
|
they can be nested!
|
2013-07-16 05:22:48 +00:00
|
|
|
|#
|
2013-07-11 11:45:38 +00:00
|
|
|
|#
|
|
|
|
|
2013-07-16 05:46:06 +00:00
|
|
|
;; S-expression comments discard the following expression,
|
|
|
|
;; useful to comment expressions when debugging
|
|
|
|
#; (this expression is discarded)
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; 1. Primitive Datatypes and Operators
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
;;; Numbers
|
2013-07-13 12:37:49 +00:00
|
|
|
9999999999999999999999 ; integers
|
2013-07-12 11:33:34 +00:00
|
|
|
3.14 ; reals
|
2013-07-11 11:45:38 +00:00
|
|
|
6.02e+23
|
2013-07-16 05:22:48 +00:00
|
|
|
1/2 ; rationals
|
|
|
|
1+2i ; complex numbers
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
;; Function application is written (f x y z ...)
|
2013-07-16 05:22:11 +00:00
|
|
|
;; 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
|
2013-07-14 11:59:16 +00:00
|
|
|
'(+ 1 2) ; => (+ 1 2)
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Now, some arithmetic operations
|
2013-07-11 11:45:38 +00:00
|
|
|
(+ 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
|
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
;;; Booleans
|
|
|
|
#t ; for true
|
2013-07-15 02:44:48 +00:00
|
|
|
#f ; for false -- any value other than #f is true
|
2013-07-11 11:45:38 +00:00
|
|
|
(not #t) ; => #f
|
2013-07-15 02:44:48 +00:00
|
|
|
(and 0 #f (error "doesn't get here")) ; => #f
|
|
|
|
(or #f 0 (error "doesn't get here")) ; => 0
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
;;; Characters
|
2013-07-11 11:45:38 +00:00
|
|
|
#\A ; => #\A
|
2013-07-16 05:22:48 +00:00
|
|
|
#\λ ; => #\λ
|
2013-07-11 11:45:38 +00:00
|
|
|
#\u03BB ; => #\λ
|
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
;;; Strings are fixed-length array of characters.
|
2013-07-11 11:45:38 +00:00
|
|
|
"Hello, world!"
|
2013-07-16 05:36:52 +00:00
|
|
|
"Benjamin \"Bugsy\" Siegel" ; backslash is an escaping character
|
|
|
|
"Foo\tbar\41\x21\u0021\a\r\n" ; includes C escapes, Unicode
|
2013-07-16 06:03:51 +00:00
|
|
|
"λx:(μα.α→α).xx" ; can include Unicode characters
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Strings can be added too!
|
2013-07-11 11:45:38 +00:00
|
|
|
(string-append "Hello " "world!") ; => "Hello world!"
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; A string can be treated like a list of characters
|
2013-07-11 11:45:38 +00:00
|
|
|
(string-ref "Apple" 0) ; => #\A
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; format can be used to format strings:
|
2013-07-11 11:45:38 +00:00
|
|
|
(format "~a can be ~a" "strings" "formatted")
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Printing is pretty easy
|
2013-07-11 11:45:38 +00:00
|
|
|
(printf "I'm Racket. Nice to meet you!\n")
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-12 11:33:34 +00:00
|
|
|
;; 2. Variables
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-16 05:22:11 +00:00
|
|
|
;; You can create a variable using define
|
|
|
|
;; a variable name can use any character except: ()[]{}",'`;#|\
|
2013-07-11 11:45:38 +00:00
|
|
|
(define some-var 5)
|
|
|
|
some-var ; => 5
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; You can also use unicode characters
|
2013-07-15 02:44:48 +00:00
|
|
|
(define ⊆ subset?)
|
2013-07-16 05:55:29 +00:00
|
|
|
(⊆ (set 3 2) (set 1 2 3)) ; => #t
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Accessing a previously unassigned variable is an exception
|
|
|
|
; x ; => x: undefined ...
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Local binding: `me' is bound to "Bob" only within the (let ...)
|
2013-07-12 11:33:34 +00:00
|
|
|
(let ([me "Bob"])
|
2013-07-16 05:28:20 +00:00
|
|
|
"Alice"
|
|
|
|
me) ; => "Bob"
|
2013-07-11 11:45:38 +00:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-14 11:59:16 +00:00
|
|
|
;; 3. Structs and Collections
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Structs
|
2013-07-14 11:59:16 +00:00
|
|
|
(struct dog (name breed age))
|
2013-07-16 05:22:48 +00:00
|
|
|
(define my-pet
|
2013-07-14 11:59:16 +00:00
|
|
|
(dog "lassie" "collie" 5))
|
|
|
|
my-pet ; => #<dog>
|
|
|
|
(dog? my-pet) ; => #t
|
|
|
|
(dog-name my-pet) ; => "lassie"
|
|
|
|
|
2013-07-15 02:44:48 +00:00
|
|
|
;;; Pairs (immutable)
|
2013-07-16 05:43:42 +00:00
|
|
|
;; `cons' constructs pairs, `car' and `cdr' extract the first
|
2013-07-16 05:22:11 +00:00
|
|
|
;; and second elements
|
2013-07-14 11:59:16 +00:00
|
|
|
(cons 1 2) ; => '(1 . 2)
|
|
|
|
(car (cons 1 2)) ; => 1
|
|
|
|
(cdr (cons 1 2)) ; => 2
|
|
|
|
|
2013-07-15 02:44:48 +00:00
|
|
|
;;; Lists
|
2013-07-13 12:37:49 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Lists are linked-list data structures
|
2013-07-15 02:44:48 +00:00
|
|
|
(list 1 2 3) ; => '(1 2 3)
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Use `cons' to add an item to the beginning of a list
|
2013-07-11 11:45:38 +00:00
|
|
|
(cons 4 '(1 2 3)) ; => (4 1 2 3)
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Use `append' to add lists together
|
2013-07-11 11:45:38 +00:00
|
|
|
(append '(1 2) '(3 4)) ; => (1 2 3 4)
|
|
|
|
|
2013-07-15 02:44:48 +00:00
|
|
|
;;; Vectors
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Vectors are fixed-length arrays
|
2013-07-15 02:44:48 +00:00
|
|
|
#(1 2 3) ; => '#(1 2 3)
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Use `vector-append' to add vectors together
|
2013-07-15 02:44:48 +00:00
|
|
|
(vector-append #(1 2 3) #(4 5 6)) ; => #(1 2 3 4 5 6)
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-13 12:37:49 +00:00
|
|
|
;;; Sets
|
|
|
|
|
2013-07-16 05:55:29 +00:00
|
|
|
;; Create a set from a list
|
2013-07-13 12:37:49 +00:00
|
|
|
(list->set '(1 2 3 1 2 3 3 2 1 3 2 1)) ; => (set 1 2 3)
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Add a member with `set-add'
|
2013-07-16 05:55:29 +00:00
|
|
|
;; (Functional: returns the extended set rather than mutate the input)
|
|
|
|
(set-add (set 1 2 3) 4) ; => (set 1 2 3 4)
|
2013-07-13 12:37:49 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Remove one with `set-remove'
|
2013-07-13 12:37:49 +00:00
|
|
|
(set-remove (set 1 2 3) 1) ; => (set 2 3)
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Test for existence with `set-member?'
|
2013-07-13 12:37:49 +00:00
|
|
|
(set-member? (set 1 2 3) 1) ; => #t
|
|
|
|
(set-member? (set 1 2 3) 4) ; => #f
|
|
|
|
|
|
|
|
;;; Hashes
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:55:29 +00:00
|
|
|
;; Create an immutable hash table (mutable example below)
|
2013-07-13 12:37:49 +00:00
|
|
|
(define m (hash 'a 1 'b 2 'c 3))
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Retrieve a value
|
2013-07-13 12:37:49 +00:00
|
|
|
(hash-ref m 'a) ; => 1
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Retrieving a non-present value is an exception
|
2013-07-13 12:37:49 +00:00
|
|
|
; (hash-ref m 'd) => no value found
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; You can provide a default value for missing keys
|
2013-07-13 12:37:49 +00:00
|
|
|
(hash-ref m 'd 0) ; => 0
|
|
|
|
|
2013-07-16 05:55:29 +00:00
|
|
|
;; Use `hash-set' to extend an immutable hash table
|
|
|
|
;; (Returns the extended hash instdead of mutating it)
|
2013-07-16 05:22:48 +00:00
|
|
|
(define m2 (hash-set m 'd 4))
|
2013-07-13 12:37:49 +00:00
|
|
|
m2 ; => '#hash((b . 2) (a . 1) (d . 4) (c . 3))
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Remember, these hashes are immutable!
|
2013-07-16 05:55:29 +00:00
|
|
|
m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
|
2013-07-13 12:37:49 +00:00
|
|
|
|
2013-07-16 05:55:29 +00:00
|
|
|
;; Use `hash-remove' to remove keys (functional too)
|
2013-07-13 12:37:49 +00:00
|
|
|
(hash-remove m 'a) ; => '#hash((b . 2) (c . 3))
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:55:29 +00:00
|
|
|
;; 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)
|
|
|
|
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; 3. Functions
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
;; Use lambda to create new functions.
|
2013-07-16 05:22:11 +00:00
|
|
|
;; A function always returns its last statement.
|
2013-07-11 11:45:38 +00:00
|
|
|
(lambda () "Hello World") ; => #<procedure>
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; (You need extra parens to call it)
|
2013-07-11 11:45:38 +00:00
|
|
|
((lambda () "Hello World")) ; => "Hello World"
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Assign a function to a var
|
2013-07-11 11:45:38 +00:00
|
|
|
(define hello-world (lambda () "Hello World"))
|
|
|
|
(hello-world) ; => "Hello World"
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; You can shorten this to:
|
2013-07-11 12:01:33 +00:00
|
|
|
(define (hello-world2) "Hello World")
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; The () is the list of arguments for the function.
|
2013-07-16 05:22:48 +00:00
|
|
|
(define hello
|
2013-07-11 11:45:38 +00:00
|
|
|
(lambda (name)
|
|
|
|
(string-append "Hello " name)))
|
|
|
|
(hello "Steve") ; => "Hello Steve"
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; You can have multi-variadic functions, too
|
2013-07-11 11:45:38 +00:00
|
|
|
(define hello2
|
2013-07-16 05:22:48 +00:00
|
|
|
(case-lambda
|
2013-07-11 11:45:38 +00:00
|
|
|
[() "Hello World"]
|
|
|
|
[(name) (string-append "Hello " name)]))
|
|
|
|
(hello2 "Jake") ; => "Hello Jake"
|
|
|
|
(hello2) ; => "Hello World"
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Functions can pack extra arguments up in a list
|
2013-07-11 11:45:38 +00:00
|
|
|
(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)"
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; You can mix regular and packed arguments
|
2013-07-11 11:45:38 +00:00
|
|
|
(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"
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-15 02:44:48 +00:00
|
|
|
;; 4. Equality
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; for numbers use `='
|
2013-07-15 02:44:48 +00:00
|
|
|
(= 3 3.0) ; => #t
|
|
|
|
(= 2 1) ; => #f
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; for object identity use `eq?'
|
2013-07-15 02:44:48 +00:00
|
|
|
(eq? 3 3) ; => #t
|
|
|
|
(eq? 3 3.0) ; => #f
|
|
|
|
(eq? (list 3) (list 3)) ; => #f
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; for collections use `equal?'
|
2013-07-15 02:44:48 +00:00
|
|
|
(equal? (list 'a 'b) (list 'a 'b)) ; => #t
|
|
|
|
(equal? (list 'a 'b) (list 'b 'a)) ; => #f
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; 5. Control Flow
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-13 12:37:49 +00:00
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
;;; Conditionals
|
|
|
|
|
2013-07-12 11:33:34 +00:00
|
|
|
(if #t ; test expression
|
|
|
|
"this is true" ; then expression
|
2013-07-16 05:28:20 +00:00
|
|
|
"this is false") ; else expression
|
|
|
|
; => "this is true"
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; In conditionals, all non-#f values are treated as true
|
2013-07-16 06:03:51 +00:00
|
|
|
(member 'Groucho '(Harpo Groucho Zeppo)) ; => '(Groucho Zeppo)
|
|
|
|
(if (member 'Groucho '(Harpo Groucho Zeppo))
|
2013-07-16 05:28:20 +00:00
|
|
|
'yep
|
|
|
|
'nope)
|
|
|
|
; => 'yep
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; `cond' chains a series of tests to select a result
|
2013-07-16 05:28:20 +00:00
|
|
|
(cond [(> 2 2) (error "wrong!")]
|
|
|
|
[(< 2 2) (error "wrong again!")]
|
|
|
|
[else 'ok]) ; => 'ok
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
;;; Pattern Matching
|
|
|
|
|
|
|
|
(define (fizzbuzz? n)
|
|
|
|
(match (list (remainder n 3) (remainder n 5))
|
|
|
|
[(list 0 0) 'fizzbuzz]
|
|
|
|
[(list 0 _) 'fizz]
|
|
|
|
[(list _ 0) 'buzz]
|
|
|
|
[_ #f]))
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
(fizzbuzz? 15) ; => 'fizzbuzz
|
2013-07-16 05:22:48 +00:00
|
|
|
(fizzbuzz? 37) ; => #f
|
2013-07-14 11:59:16 +00:00
|
|
|
|
|
|
|
;;; Loops
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; looping can be done through recursion
|
2013-07-14 11:59:16 +00:00
|
|
|
(define (loop i)
|
|
|
|
(when (< i 10)
|
2013-07-16 05:36:52 +00:00
|
|
|
(printf "i:~a\n" i)
|
2013-07-16 05:22:48 +00:00
|
|
|
(loop (add1 i))))
|
2013-07-14 11:59:16 +00:00
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
(loop 5) ; => i:5 i:6 ...
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; similarly, with a named let
|
2013-07-14 11:59:16 +00:00
|
|
|
(let loop ((i 0))
|
|
|
|
(when (< i 10)
|
2013-07-16 05:36:52 +00:00
|
|
|
(printf "i:~a\n" i)
|
2013-07-14 11:59:16 +00:00
|
|
|
(loop (add1 i)))) ; => i:0 i:1 ...
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-15 02:44:48 +00:00
|
|
|
;;; Comprehensions
|
|
|
|
|
|
|
|
(for/list ([i '(1 2 3)])
|
2013-07-16 05:28:20 +00:00
|
|
|
(add1 i)) ; => '(2 3 4)
|
2013-07-15 02:44:48 +00:00
|
|
|
|
|
|
|
(for/list ([i '(1 2 3)] #:when (even? i))
|
2013-07-16 05:28:20 +00:00
|
|
|
i) ; => '(2)
|
2013-07-15 02:44:48 +00:00
|
|
|
|
|
|
|
(for/hash ([i '(1 2 3)])
|
2013-07-16 06:03:51 +00:00
|
|
|
(values i (number->string i)))
|
|
|
|
; => '#hash((1 . "1") (2 . "2") (3 . "3"))
|
2013-07-15 02:44:48 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; To combine iteration results, use `for/fold'
|
2013-07-15 02:44:48 +00:00
|
|
|
(for/fold ([sum 0]) ([i '(1 2 3 4)])
|
|
|
|
(+ sum i)) ; => 10
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
;;; Sequences
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; `for' allows iteration over sequences:
|
2013-07-16 05:22:48 +00:00
|
|
|
;; lists, vectors, strings, sets, hash tables, etc...
|
2013-07-14 11:59:16 +00:00
|
|
|
(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 ))])
|
2013-07-16 05:36:52 +00:00
|
|
|
(printf "key:~a value:~a\n" k v))
|
2013-07-14 11:59:16 +00:00
|
|
|
|
|
|
|
;;; Exceptions
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; To catch an exception, use the `with-handlers' form
|
|
|
|
;; To throw an exception use `raise'
|
2013-07-16 05:22:48 +00:00
|
|
|
(with-handlers
|
|
|
|
([(lambda (v) (equal? v "infinity"))
|
2013-07-12 11:33:34 +00:00
|
|
|
(lambda (exn) +inf.0)])
|
|
|
|
(raise "infinity"))
|
2013-07-11 11:45:38 +00:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-15 02:44:48 +00:00
|
|
|
;; 6. Mutation
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Use set! to assign a new value to an existing variable
|
2013-07-15 02:44:48 +00:00
|
|
|
(define n 5)
|
2013-07-16 05:22:48 +00:00
|
|
|
(set! n 6)
|
2013-07-15 02:44:48 +00:00
|
|
|
n ; => 6
|
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
;; Many Racket datatypes can be immutable or mutable
|
2013-07-16 05:22:11 +00:00
|
|
|
;; (Pairs, Lists, Strings, Vectors, Hash Tables, etc...)
|
2013-07-15 02:44:48 +00:00
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Use `vector' to create a mutable vector
|
2013-07-15 02:44:48 +00:00
|
|
|
(define vec (vector 2 2 3 4))
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Use vector-set! to update a slot
|
2013-07-15 02:44:48 +00:00
|
|
|
(vector-set! vec 0 1)
|
|
|
|
vec ; => #(1 2 3 4)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; 7. Modules
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 06:03:51 +00:00
|
|
|
;; 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)
|
2013-07-14 11:59:16 +00:00
|
|
|
|
2013-07-16 06:03:51 +00:00
|
|
|
(module cake racket/base ; define a `cake' module based on racket/base
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-14 11:59:16 +00:00
|
|
|
(provide print-cake) ; function exported by the module
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-12 11:33:34 +00:00
|
|
|
(define (print-cake n)
|
|
|
|
(show " ~a " n #\.)
|
|
|
|
(show " .-~a-. " n #\|)
|
|
|
|
(show " | ~a | " n #\space)
|
|
|
|
(show "---~a---" n #\-))
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-16 05:29:37 +00:00
|
|
|
(define (show fmt n ch) ; internal function
|
2013-07-12 11:33:34 +00:00
|
|
|
(printf fmt (make-string n ch))
|
|
|
|
(newline)))
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Use `require' to import all functions from the module
|
2013-07-16 05:22:48 +00:00
|
|
|
(require 'cake)
|
|
|
|
(print-cake 3)
|
2013-07-16 05:43:42 +00:00
|
|
|
; (show "~a" 1 #\A) ; => error, `show' was not exported
|
2013-07-11 11:45:38 +00:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-15 02:44:48 +00:00
|
|
|
;; 8. Classes and Objects
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Create a class fish%
|
2013-07-13 12:37:49 +00:00
|
|
|
(define fish%
|
2013-07-16 05:22:48 +00:00
|
|
|
(class object%
|
2013-07-13 12:37:49 +00:00
|
|
|
(init size) ; initialization argument
|
2013-07-16 05:22:48 +00:00
|
|
|
(super-new) ; superclass initialization
|
2013-07-16 05:29:37 +00:00
|
|
|
;; Field
|
2013-07-16 05:22:48 +00:00
|
|
|
(define current-size size)
|
2013-07-16 05:29:37 +00:00
|
|
|
;; Public methods
|
2013-07-16 06:03:51 +00:00
|
|
|
(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)))))
|
2013-07-13 12:37:49 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Create an instance of fish%
|
2013-07-16 05:22:48 +00:00
|
|
|
(define charlie
|
2013-07-13 12:37:49 +00:00
|
|
|
(new fish% [size 10]))
|
|
|
|
|
2013-07-16 05:43:42 +00:00
|
|
|
;; Use `send' to call an object's methods
|
2013-07-13 12:37:49 +00:00
|
|
|
(send charlie grow 6)
|
|
|
|
(send charlie get-size) ; => 16
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-15 02:44:48 +00:00
|
|
|
;; 9. Macros
|
2013-07-11 11:45:38 +00:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2013-07-14 11:59:16 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Macros let you extend the syntax of the language
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Let's add a while loop
|
2013-07-15 22:51:38 +00:00
|
|
|
(define-syntax-rule (while condition body ...)
|
|
|
|
(let loop ()
|
|
|
|
(when condition
|
|
|
|
body ...
|
|
|
|
(loop))))
|
|
|
|
|
|
|
|
(let ([i 0])
|
|
|
|
(while (< i 10)
|
|
|
|
(displayln i)
|
|
|
|
(set! i (add1 i))))
|
2013-07-12 11:33:34 +00:00
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
;; Macros are hygienic, you cannot clobber existing variables!
|
2013-07-12 11:33:34 +00:00
|
|
|
(define-syntax-rule (swap x y)
|
2013-07-16 05:30:51 +00:00
|
|
|
(let ([tmp x])
|
2013-07-12 11:33:34 +00:00
|
|
|
(set! x y)
|
|
|
|
(set! y tmp)))
|
|
|
|
|
2013-07-16 05:22:48 +00:00
|
|
|
(define tmp 1)
|
2013-07-12 11:33:34 +00:00
|
|
|
(define a 2)
|
|
|
|
(define b 3)
|
|
|
|
(swap a b)
|
2013-07-16 06:03:51 +00:00
|
|
|
(printf "tmp = ~a; a = ~a; b = ~a\n" tmp a b) ; tmp is unaffected
|
2013-07-15 02:44:48 +00:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; 10. Contracts
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-07-16 05:22:11 +00:00
|
|
|
;; Contracts impose constraints on values exported from modules
|
2013-07-15 02:44:48 +00:00
|
|
|
|
|
|
|
(module bank-account racket
|
|
|
|
(provide (contract-out
|
2013-07-16 06:03:51 +00:00
|
|
|
[deposit (-> positive? any)] ; amounts are always positive
|
2013-07-15 02:44:48 +00:00
|
|
|
[balance (-> positive?)]))
|
2013-07-16 05:22:48 +00:00
|
|
|
|
2013-07-15 02:44:48 +00:00
|
|
|
(define amount 0)
|
|
|
|
(define (deposit a) (set! amount (+ amount a)))
|
|
|
|
(define (balance) amount)
|
2013-07-16 05:28:20 +00:00
|
|
|
)
|
2013-07-15 02:44:48 +00:00
|
|
|
|
|
|
|
(require 'bank-account)
|
|
|
|
(deposit 5)
|
|
|
|
|
|
|
|
(balance) ; => 5
|
|
|
|
|
2013-07-16 06:03:51 +00:00
|
|
|
;; Clients that attempt to deposit a non-positive amount are blamed
|
2013-07-16 05:22:48 +00:00
|
|
|
;; (deposit -5) ; => deposit: contract violation
|
2013-07-16 05:22:11 +00:00
|
|
|
;; expected: positive?
|
2013-07-16 05:22:48 +00:00
|
|
|
;; given: -5
|
2013-07-16 05:22:11 +00:00
|
|
|
;; more details....
|
2013-07-11 11:45:38 +00:00
|
|
|
```
|
|
|
|
|
2013-07-13 12:37:49 +00:00
|
|
|
## Further Reading
|
2013-07-11 11:45:38 +00:00
|
|
|
|
2013-07-16 06:04:56 +00:00
|
|
|
Still up for more? Try [Getting Started with Racket](http://docs.racket-lang.org/getting-started/)
|