* irrational numbers
** strings
* "tutti frutti"
- * (= (string-append "tutti" "frutti") "tuttifrutti")
- * (= (string-append "tutti" " " "frutti") "tutti frutti")
+ * (equal? (string-append "tutti" "frutti") "tuttifrutti")
+ * (equal?(string-append "tutti" " " "frutti") "tutti frutti")
* substring, string-ref, string=?
** lists
* super duper important in racket
*** cons cells
- * (= (list 1 2) (cons 2 (cons 3 empty)))
+ * (equal?(list 1 2) (cons 2 (cons 3 empty)))
* same as linked lists in other languages
**** functions for cons cells
- * (= (cons 1 2) '(1 . 2))
- * (define cell (cons 'a 'b)) (= (car cell) 'a) (= (cdr cell) 'b)
+ * (equal?(cons 1 2) '(1 . 2))
+ * (define cell (cons 'a 'b)) (equal?(car cell) 'a) (equal?(cdr cell) 'b)
*** lists and list functions
- * (= '() empty (list))
+ * (equal?'() empty (list))
**** cons function
- * (= (cons 'chicken empty) '(chicken))
+ * (equal?(cons 'chicken empty) '(chicken))
* empty is used to terminate lists in racket, so '(chicken) has
an implicit empty
* what do you get when you add a chicken to an empty list? a list with
a chicken in it
- * (= (cons 'pork '(beef chicken)) '(pork beef chicken))
- * (= (cons 'beef (cons 'chicken '())) '(beef chicken))
- * (= (cons 'pork (cons 'beef (cons 'chicken '())))
+ * (equal?(cons 'pork '(beef chicken)) '(pork beef chicken))
+ * (equal?(cons 'beef (cons 'chicken '())) '(beef chicken))
+ * (equal?(cons 'pork (cons 'beef (cons 'chicken '())))
'(pork beef chicken))
**** list function
- * (= (list 'pork 'beef 'chicken) '(pork beef chicken))
+ * (equal?(list 'pork 'beef 'chicken) '(pork beef chicken))
**** first and rest functions
- * (= (first (cons 'pork (cons 'beef (cons 'chicken empty)))) 'pork)
- * (= (rest (list 'pork 'beef 'chicken)) '(beef chicken))
- * (= (first (rest ('pork beef chicken))) 'beef)
+ * (equal?(first (cons 'pork (cons 'beef (cons 'chicken empty)))) 'pork)
+ * (equal?(rest (list 'pork 'beef 'chicken)) '(beef chicken))
+ * (equal?(first (rest ('pork beef chicken))) 'beef)
* how do you get the third item?
* what happens when you try to get the fourth item?
**** nested lists
- * (= (list 'cat (list 'duck 'bat) 'ant) '(cat (duck bat) ant))
+ * (equal?(list 'cat (list 'duck 'bat) 'ant) '(cat (duck bat) ant))
* all lists are made of cons cells
- * (= (first '((peas carrots tomatoes) (pork beef chicken)))
+ * (equal?(first '((peas carrots tomatoes) (pork beef chicken)))
(peas carrots tomatoes))
- * (= (rest '(peas carrots tomatoes)) '(carrots tomatoes))
- * (= (rest (first '((peas carrots tomatoes) (pork beef chicken))))
+ * (equal?(rest '(peas carrots tomatoes)) '(carrots tomatoes))
+ * (equal?(rest (first '((peas carrots tomatoes) (pork beef chicken))))
'(carrots tomatoes))
* second third ... tenth built-in
+** structures
+ structures group things together, like lists. structures group a
+ fixed number of items, unlike lists.
+ example: student has name ID dorm.
+*** structure basics
+ * "(struct student (name id# dorm))"
+ this defines a structure, but doesn't actually create one.
+ * "(define freshman1 (student 'Joe 1234 'NewHall))"
+ this creates a structure
+ * fields: name, id#, dorm
+ * (equal? (student-name freshman1) 'Joe)
+*** nesting structures
+ * (struct student-body (freshmen sophomores juniors seniors))
+ * (define all-student (list a b) (list) (list c d) (list e))
+ * a b c d e can all be student struct instances
+ * nested selectors:
+ * (student-name (first (student-body-freshmen all-students)))
+*** structure transparency
+ * default is opaque
+ * opaque means comparison is by identity not value
+ * (equal? false (equal? (student 'joe 1 'dorm) (student 'joe 1 'dorm)))
+ * (equal? true (equal? freshman1 freshman1))
+ * transparent structs are compared on value
+ * create transparent struct type: (struct a (b) #:transparent)
+ * (equal? true (equal? (a 'b) (a 'b)))
+ * (define b (a b))
+ * (equal? true (equal? b b))
+ * all structures in book are transparent
+**** checkpoint
+ * basic data: booleans, symbols, numbers, strings
+ * lists
+ * structs
+ * nesting of lists and structs
+** conditions and decisions
+*** how to ask
+ * predicates are questions
+ * #t true, and #f false
+ * predicate is a function that returns true or false
+ * zero? symbol=? symbol? student?
+ * doing something like (zero? "asdf") will give an error, not false
+ * real? number? string? list? cons? empty? rational? exact-integer?
+ * equal? compares everything. still gives error if you give wrong arity
+ * the predicate indicates what kind of arguments it works on
+*** the conditionals: if and beyond
+ * (equal? 'yup (if (= (+ 1 2) 3) 'yup 'nope))
+ * invented in 1960s by john mccarthy
+ * #f is false and everything else is true, including '()
+ * examples
+ * (if '(1) 'consequent 'alternative) 'consequent
+ * (if '() 'consequent 'alternative) 'consequent
+ * (if #f 'consequent 'alternative) 'alternative
+ * ((if 'a
+ (display "consequent")
+ (display "alternative"))) ;; prints "consequent"