start notes on conditions
[ozzloy@gmail.com/realm-of-racket-journey] / notes.org
CommitLineData
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
13572486 88** conditions and decisions
89*** how to ask
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
98*** the conditionals: if and beyond
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
106 * ((if 'a
107 (display "consequent")
108 (display "alternative"))) ;; prints "consequent"