complete chapter 3 notes
[ozzloy@gmail.com/realm-of-racket-journey] / notes.org
1 * form
2 * "(function element0 ...)"
3 * building blocks of racket semantics
4 ** booleans
5 * "(zero? 1)" "(zero? (sub1 1))"
6 ** symbols
7 * a-z+-/*=<>?!_^
8 * (symbol=? 'foo 'FoO) true or false?
9 ** numbers
10 * "(expt 53 53)" "(= (/ 4 6) 2/3 (/ 2 3))" "(= (/ 4.0 6) 0.6666666666666666)"
11 * numbers like 4.0 are floating point numbers
12 * irrational numbers
13 ** strings
14 * "tutti frutti"
15 * (equal? (string-append "tutti" "frutti") "tuttifrutti")
16 * (equal?(string-append "tutti" " " "frutti") "tutti frutti")
17 * substring, string-ref, string=?
18 ** lists
19 * super duper important in racket
20 *** cons cells
21 * (equal?(list 1 2) (cons 2 (cons 3 empty)))
22 * same as linked lists in other languages
23 **** functions for cons cells
24 * (equal?(cons 1 2) '(1 . 2))
25 * (define cell (cons 'a 'b)) (equal?(car cell) 'a) (equal?(cdr cell) 'b)
26 *** lists and list functions
27 * (equal?'() empty (list))
28 **** cons function
29 * (equal?(cons 'chicken empty) '(chicken))
30 * empty is used to terminate lists in racket, so '(chicken) has
31 an implicit empty
32 * what do you get when you add a chicken to an empty list? a list with
33 a chicken in it
34 * (equal?(cons 'pork '(beef chicken)) '(pork beef chicken))
35 * (equal?(cons 'beef (cons 'chicken '())) '(beef chicken))
36 * (equal?(cons 'pork (cons 'beef (cons 'chicken '())))
37 '(pork beef chicken))
38 **** list function
39 * (equal?(list 'pork 'beef 'chicken) '(pork beef chicken))
40 **** first and rest functions
41 * (equal?(first (cons 'pork (cons 'beef (cons 'chicken empty)))) 'pork)
42 * (equal?(rest (list 'pork 'beef 'chicken)) '(beef chicken))
43 * (equal?(first (rest ('pork beef chicken))) 'beef)
44 * how do you get the third item?
45 * what happens when you try to get the fourth item?
46 **** nested lists
47 * (equal?(list 'cat (list 'duck 'bat) 'ant) '(cat (duck bat) ant))
48 * all lists are made of cons cells
49 * (equal?(first '((peas carrots tomatoes) (pork beef chicken)))
50 (peas carrots tomatoes))
51 * (equal?(rest '(peas carrots tomatoes)) '(carrots tomatoes))
52 * (equal?(rest (first '((peas carrots tomatoes) (pork beef chicken))))
53 '(carrots tomatoes))
54 * second third ... tenth built-in
55 ** structures
56 structures group things together, like lists. structures group a
57 fixed number of items, unlike lists.
58 example: student has name ID dorm.
59 *** structure basics
60 * "(struct student (name id# dorm))"
61 this defines a structure, but doesn't actually create one.
62 * "(define freshman1 (student 'Joe 1234 'NewHall))"
63 this creates a structure
64 * fields: name, id#, dorm
65 * (equal? (student-name freshman1) 'Joe)
66 *** nesting structures
67 * (struct student-body (freshmen sophomores juniors seniors))
68 * (define all-student (list a b) (list) (list c d) (list e))
69 * a b c d e can all be student struct instances
70 * nested selectors:
71 * (student-name (first (student-body-freshmen all-students)))
72 *** structure transparency
73 * default is opaque
74 * opaque means comparison is by identity not value
75 * (equal? false (equal? (student 'joe 1 'dorm) (student 'joe 1 'dorm)))
76 * (equal? true (equal? freshman1 freshman1))
77 * transparent structs are compared on value
78 * create transparent struct type: (struct a (b) #:transparent)
79 * (equal? true (equal? (a 'b) (a 'b)))
80 * (define b (a b))
81 * (equal? true (equal? b b))
82 * all structures in book are transparent
83 **** checkpoint
84 * basic data: booleans, symbols, numbers, strings
85 * lists
86 * structs
87 * nesting of lists and structs