This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| +; <procedure +> | |
| square; <procedure square> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def square(x): return x*x | |
| square # <function __main__.square(x)> | |
| # `+` is syntax in Python — it cannot be referenced as a value (SyntaxError) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define abs | |
| (lambda (x) (cond [(< x 0) (- x)] [(= x 0) 0] [(> x 0) x]))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (assert (= (abs -20) 20)) | |
| (assert (= (abs 0) 0)) | |
| (assert (= (abs 20) 20)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define abs2 | |
| (lambda (x) | |
| (if (< x 0) (- x) x))) | |
| (assert (= (abs2 -5) 5)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (sqrt x) | |
| (define avg | |
| (lambda (x y) | |
| (/ (+ x y) 2))) | |
| (define (square x) | |
| (* x x)) | |
| (define abs | |
| (lambda (x) | |
| (cond | |
| [(< x 0) (- x)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; avg/square/abs/improve are scoped inside sqrt — verified values: | |
| ; (avg 0 100)=>50 (square 3)=>9 (abs -1)=>1 (improve 50 100)=>26 | |
| (assert (< (abs (- (sqrt 9) 3)) 0.001)) ; ✅ approx check — sqrt returns 65537/21845, not exactly 3 | |
| (sqrt 9) ; => 65537/21845 ≈ 3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TO FIND AN APPROXIMATION TO SQUARE ROOT OF X (9): | |
| • MAKE A GUESS G (1) | |
| • IMPROVE THE GUESS BY AVERAGING G AND X/G (1+(9/1))/2 = 5; 👇 | |
| • KEEP IMPROVING THE GUESS UNTIL IT IS GOOD ENOUGH (5+(9/5))/2 = 3,4; (3,4+(9/3/4))/2 = 3.02 ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GENERAL | |
| METHOD FOR FINDING A FIXED POINT OF A FUNCTION F | |
| (THAT IS, A VALUE Y SUCH THAT F(Y) = Y): | |
| • START WITH A GUESS FOR Y | |
| • KEEP APPLYING F OVER AND OVER UNTIL THE RESULT DOESN'T CHANGE VERY MUCH. | |
| # APPLIED | |
| TO COMPUTE A SQUARE ROOT OF X, FIND A >>FIXED POINT<< | |
| OF THE FUNCTION F, WHICH TAKES Y OVER THE AVERAGE OF Y AND X/Y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TO FIND AN APPROXIMATION TO SQUARE ROOT OF X (e.g. 9): | |
| • MAKE A GUESS G (start: 1) | |
| • IMPROVE: average G and X/G → (1 + 9/1) / 2 = 5 | |
| • KEEP IMPROVING UNTIL GOOD ENOUGH: | |
| (5 + 9/5) / 2 = 3.4 | |
| (3.4 + 9/3.4) / 2 ≈ 3.02 ✅ |