add up to 4-and-a-half.1
[ozzloy@gmail.com/realm-of-racket-journey] / notes.org
index ebc40bf4477f44d96fecf242b109f6e60a97f49e..e51fd62abdd7dd2a14b34c37fb3f528f6527980d 100644 (file)
--- a/notes.org
+++ b/notes.org
    * 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
+** 4 conditions and decisions
+*** 4.1 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
+*** 4.2 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 (display "condition")
+          (display "consequent")
+          (display "alternative")) ;; prints "conditionconsequent"
+    * cond is like if else-if else-if ...
+    * (cond [condition0 consequent0] [condition1 consequent1] [else default])
+**** a first taste of recursion
+     * (define (my-length a-list)
+         (if (empty? a-list)
+             0
+             (add1 (my-length (rest a-list)))))
+     * this is called a "list-eater"
+*** 4.3 cool tricks with conditionals
+**** using the stealth conditionals AND and OR
+     * AKA short-circuiting conditionals
+     * (if file-modified
+           (if (ask-user-about-saving)
+               (save-file)
+               false)
+           false)
+     * (and file-modified (ask-user-about-saving) (save-file))
+     * (when (and file-modified (ask-user-about-saving))
+         (save-file))
+     * unless is when with not.  (unless condition action)
+**** using functions that return more than just the truth
+     * (member 1 '(1 2)) ;; '(1 2)
+     * (member 2 '(1 2)) ;; '(2)
+*** 4.4 equality predicates, once more
+    * (struct point (x y) #:transparent)
+    * point point-x point-y point?
+    * (define (distance-to-origin p)
+        (sqrt (+ (sqr (point-x p)) (sqr (point-y p)))))
+    * (distance-to-origin (point 3 4))
+    * give names to points using define:
+      * (define pt1 (point -1 2))
+      * (define pt2 (point -1 2))
+    * equal? checks equality of all components of a struct
+      * (equal? pt1 pt2)
+    * how would you write equal?
+    * (define (my-equal? a b)
+        (cond
+          [(and (point? a) (point? b))
+           (and (my-equal? (point-x a) (point-x b))
+                (my-equal? (point-y a) (point-y b)))] ... ))
+    * you need another cond clause for every struct, and primitive forms
+      of data, like boolean, numbers, strings
+    * whenever a new struct is added, equal? definition must be updated
+    * to compare whether two concrete structures are the same piece of
+      memory, use eq?
+    * (eq? pt1 pt2) #f
+    * (eq? pt1 pt1) #t
+    * construct a scenario where (eq? a b) evaluates to #t
+    * you can give new names to existing values with define
+      * (define pt3 pt1)
+    * construct a scenario where (eq? a b) evaluates to #t
+      * do not use define to give a new name to a value
+    * (define (eq-first-items list1 list2)
+        (eq? (first list1) (first list2)))
+    * (eq-first-items (cons pt1 empty) (cons pt3 empty))
+    * equal? checks if things are made of equal values
+    * eq? checks whether two names refer to the same exact thing
+*** 4.5 comparing and testing
+    * write tests before writing functions
+    * test is function call and expected result value
+    * racket compares return value with expected
+      * if dissimilar, test or function or both are wrong
+    * (require rackunit)
+**** writing a test
+     * check-equal? is most common
+     * (check-equal? (add1 5) 6)
+     * says nothing if test passes
+     * throws failure if test fails
+     * (check-equal? (add1 5) 7)
+**** what is not a test
+     * running a function or program by hand is not a test
+       * though it's ok to explore a function by hand
+     * writing down expectation afterwards
+     * no automatic comparison
+**** testing in the real world
+     * tests take an optional 3rd argument string to clarify
+     * (check-equal? 5 6 "numbers matter")
+     * check-not-equal? check-pred check-= check-true check-false
+**** call-with-current-continuation - chapter checkpoint pp 70
+     * racket has predicates of type and equality
+     * if and cond are the most important conditionals
+** 4-and-a-half (define define 'define)
+*** 4-and-a-half.1 module level definitions
+    * most common kind of definitions are module-level
+      * introduce names that can be accessed anywhere in the module
+    * think of module as definitions panel
+    * variable and function definitions