add up to 4-and-a-half.1 master
authordaniel watson <ozzloy@gmail.com>
Wed, 24 Aug 2016 20:50:26 +0000 (13:50 -0700)
committerdaniel watson <ozzloy@gmail.com>
Wed, 24 Aug 2016 20:50:26 +0000 (13:50 -0700)
notes.org

index 62c078860fc87d9ca66f8d110da703aa42e3b9ad..e51fd62abdd7dd2a14b34c37fb3f528f6527980d 100644 (file)
--- a/notes.org
+++ b/notes.org
@@ -85,8 +85,8 @@
      * lists
      * structs
      * nesting of lists and structs
-** conditions and decisions
-*** how to ask
+** 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
@@ -95,7 +95,7 @@
     * 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
+*** 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 '()
       * (if '(1) 'consequent 'alternative) 'consequent
       * (if '() 'consequent 'alternative) 'consequent
       * (if #f 'consequent 'alternative) 'alternative
-    * ((if 'a
-           (display "consequent")
-           (display "alternative"))) ;; prints "consequent" 
+    * (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