summaryrefslogtreecommitdiffhomepage
path: root/racket.html.markdown
blob: 8cc5ac24695baaecdd80e6708c6e471364f4446b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
---
language: racket
author: th3rac25
---

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]


```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
    as comments
|#

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1. Primitive Datatypes and Operators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Numbers
9999999999999999999999 ; integers
3.14                   ; reals
6.02e+23
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

; Here are a few arithmetic operators
(+ 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

; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 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 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 ...

; Local binding: me is bound to "Bob" only within (let ...)
(let ([me "Bob"])
    "Alice"
    me) ; => "Bob"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 3. Collections
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; 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
(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

;;; 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))

; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Use lambda to create new functions. A function always returns
; its last statement.
(lambda () "Hello World") ; => #<procedure>

; (You need extra parens to call it)
((lambda () "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:
(define (hello-world2) "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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; 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 tests 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)))

;; Use "require" to import all functions from the module
(require 'cake) 
(print-cake 3)  
;(show "~a" 1 #\A) ; => error, "show" was not exported

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 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

Still up for more? Try [Quick: An Introduction to Racket with Pictures](http://docs.racket-lang.org/quick/)