8715a946 |
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" |
af8e2df9 |
15 | * (equal? (string-append "tutti" "frutti") "tuttifrutti") |
16 | * (equal?(string-append "tutti" " " "frutti") "tutti frutti") |
8715a946 |
17 | * substring, string-ref, string=? |
18 | ** lists |
19 | * super duper important in racket |
20 | *** cons cells |
af8e2df9 |
21 | * (equal?(list 1 2) (cons 2 (cons 3 empty))) |
8715a946 |
22 | * same as linked lists in other languages |
23 | **** functions for cons cells |
af8e2df9 |
24 | * (equal?(cons 1 2) '(1 . 2)) |
25 | * (define cell (cons 'a 'b)) (equal?(car cell) 'a) (equal?(cdr cell) 'b) |
8715a946 |
26 | *** lists and list functions |
af8e2df9 |
27 | * (equal?'() empty (list)) |
8715a946 |
28 | **** cons function |
af8e2df9 |
29 | * (equal?(cons 'chicken empty) '(chicken)) |
8715a946 |
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 |
af8e2df9 |
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 '()))) |
8715a946 |
37 | '(pork beef chicken)) |
38 | **** list function |
af8e2df9 |
39 | * (equal?(list 'pork 'beef 'chicken) '(pork beef chicken)) |
8715a946 |
40 | **** first and rest functions |
af8e2df9 |
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) |
8715a946 |
44 | * how do you get the third item? |
45 | * what happens when you try to get the fourth item? |
46 | **** nested lists |
af8e2df9 |
47 | * (equal?(list 'cat (list 'duck 'bat) 'ant) '(cat (duck bat) ant)) |
8715a946 |
48 | * all lists are made of cons cells |
af8e2df9 |
49 | * (equal?(first '((peas carrots tomatoes) (pork beef chicken))) |
8715a946 |
50 | (peas carrots tomatoes)) |
af8e2df9 |
51 | * (equal?(rest '(peas carrots tomatoes)) '(carrots tomatoes)) |
52 | * (equal?(rest (first '((peas carrots tomatoes) (pork beef chicken)))) |
8715a946 |
53 | '(carrots tomatoes)) |
54 | * second third ... tenth built-in |
af8e2df9 |
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) |
ecaeb60c |
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 |
0348f54d |
88 | ** 4 conditions and decisions |
89 | *** 4.1 how to ask |
13572486 |
90 | * predicates are questions |
91 | * #t true, and #f false |
92 | * predicate is a function that returns true or false |
93 | * zero? symbol=? symbol? student? |
94 | * doing something like (zero? "asdf") will give an error, not false |
95 | * real? number? string? list? cons? empty? rational? exact-integer? |
96 | * equal? compares everything. still gives error if you give wrong arity |
97 | * the predicate indicates what kind of arguments it works on |
0348f54d |
98 | *** 4.2 the conditionals: if and beyond |
13572486 |
99 | * (equal? 'yup (if (= (+ 1 2) 3) 'yup 'nope)) |
100 | * invented in 1960s by john mccarthy |
101 | * #f is false and everything else is true, including '() |
102 | * examples |
103 | * (if '(1) 'consequent 'alternative) 'consequent |
104 | * (if '() 'consequent 'alternative) 'consequent |
105 | * (if #f 'consequent 'alternative) 'alternative |
0348f54d |
106 | * (if (display "condition") |
107 | (display "consequent") |
108 | (display "alternative")) ;; prints "conditionconsequent" |
109 | * cond is like if else-if else-if ... |
110 | * (cond [condition0 consequent0] [condition1 consequent1] [else default]) |
111 | **** a first taste of recursion |
112 | * (define (my-length a-list) |
113 | (if (empty? a-list) |
114 | 0 |
115 | (add1 (my-length (rest a-list))))) |
116 | * this is called a "list-eater" |
117 | *** 4.3 cool tricks with conditionals |
118 | **** using the stealth conditionals AND and OR |
119 | * AKA short-circuiting conditionals |
120 | * (if file-modified |
121 | (if (ask-user-about-saving) |
122 | (save-file) |
123 | false) |
124 | false) |
125 | * (and file-modified (ask-user-about-saving) (save-file)) |
126 | * (when (and file-modified (ask-user-about-saving)) |
127 | (save-file)) |
128 | * unless is when with not. (unless condition action) |
129 | **** using functions that return more than just the truth |
130 | * (member 1 '(1 2)) ;; '(1 2) |
131 | * (member 2 '(1 2)) ;; '(2) |
132 | *** 4.4 equality predicates, once more |
133 | * (struct point (x y) #:transparent) |
134 | * point point-x point-y point? |
135 | * (define (distance-to-origin p) |
136 | (sqrt (+ (sqr (point-x p)) (sqr (point-y p))))) |
137 | * (distance-to-origin (point 3 4)) |
138 | * give names to points using define: |
139 | * (define pt1 (point -1 2)) |
140 | * (define pt2 (point -1 2)) |
141 | * equal? checks equality of all components of a struct |
142 | * (equal? pt1 pt2) |
143 | * how would you write equal? |
144 | * (define (my-equal? a b) |
145 | (cond |
146 | [(and (point? a) (point? b)) |
147 | (and (my-equal? (point-x a) (point-x b)) |
148 | (my-equal? (point-y a) (point-y b)))] ... )) |
149 | * you need another cond clause for every struct, and primitive forms |
150 | of data, like boolean, numbers, strings |
151 | * whenever a new struct is added, equal? definition must be updated |
152 | * to compare whether two concrete structures are the same piece of |
153 | memory, use eq? |
154 | * (eq? pt1 pt2) #f |
155 | * (eq? pt1 pt1) #t |
156 | * construct a scenario where (eq? a b) evaluates to #t |
157 | * you can give new names to existing values with define |
158 | * (define pt3 pt1) |
159 | * construct a scenario where (eq? a b) evaluates to #t |
160 | * do not use define to give a new name to a value |
161 | * (define (eq-first-items list1 list2) |
162 | (eq? (first list1) (first list2))) |
163 | * (eq-first-items (cons pt1 empty) (cons pt3 empty)) |
164 | * equal? checks if things are made of equal values |
165 | * eq? checks whether two names refer to the same exact thing |
166 | *** 4.5 comparing and testing |
167 | * write tests before writing functions |
168 | * test is function call and expected result value |
169 | * racket compares return value with expected |
170 | * if dissimilar, test or function or both are wrong |
171 | * (require rackunit) |
172 | **** writing a test |
173 | * check-equal? is most common |
174 | * (check-equal? (add1 5) 6) |
175 | * says nothing if test passes |
176 | * throws failure if test fails |
177 | * (check-equal? (add1 5) 7) |
178 | **** what is not a test |
179 | * running a function or program by hand is not a test |
180 | * though it's ok to explore a function by hand |
181 | * writing down expectation afterwards |
182 | * no automatic comparison |
183 | **** testing in the real world |
184 | * tests take an optional 3rd argument string to clarify |
185 | * (check-equal? 5 6 "numbers matter") |
186 | * check-not-equal? check-pred check-= check-true check-false |
187 | **** call-with-current-continuation - chapter checkpoint pp 70 |
188 | * racket has predicates of type and equality |
189 | * if and cond are the most important conditionals |
190 | ** 4-and-a-half (define define 'define) |
191 | *** 4-and-a-half.1 module level definitions |
192 | * most common kind of definitions are module-level |
193 | * introduce names that can be accessed anywhere in the module |
194 | * think of module as definitions panel |
195 | * variable and function definitions |