(define x 2) "x, before local" x "x, inside local" (local [(define x 5)] x) ;; x has a different definition in here. "x, after local" x (define z 10) (define (local_test1 x) (cond [(< x 5) (local [(define a 2)] (+ z (* x a)))] [else (+ z (* x a))])) " local_test1: This test works: " (= 16 (local_test1 3)) ;; Un-comment the next two lines to attempt to run the test. ;"local_test1: This test will not execute at all." ;(= 22 (local_test1 6)) ;; A BinTree is either ;; -- EmptyTree ;; -- NETree ;; A NETree ;; (make-NETree any BinTree BinTree) (define-struct NETree (first left right)) ;; EmptyTree is empty (define-struct EmptyTree ()) (define EMPTYTREE (make-EmptyTree)) (define bt (make-NETree 3 (make-NETree 5 EMPTYTREE (make-NETree 6 EMPTYTREE EMPTYTREE)) (make-NETree 2 EMPTYTREE EMPTYTREE))) ;; maxDepth_long: BinTree --> num ;; Returns the maximum depth of the tree. (define (maxDepth_long binTree) (cond [(EmptyTree? binTree) 0] [(NETree? binTree) (cond [(< (maxDepth_long (NETree-left binTree)) (maxDepth_long (NETree-right binTree))) (add1 (maxDepth_long (NETree-right binTree)))] [else (add1 (maxDepth_long (NETree-left binTree)))])])) "maxDepth_long test cases: " (= 0 (maxDepth_long EMPTYTREE)) (= 3 (maxDepth_long bt)) ;; maxDepth: BinTree --> num ;; Returns the maximum depth of the tree. (define (maxDepth binTree) (cond [(EmptyTree? binTree) 0] [(NETree? binTree) (local [(define leftDepth (maxDepth (NETree-left binTree))) (define rightDepth (maxDepth (NETree-right binTree)))] (cond [(< leftDepth rightDepth) (add1 rightDepth)] [else (add1 leftDepth)]))])) "maxDepth test cases:" (= 0 (maxDepth EMPTYTREE)) (= 3 (maxDepth bt)) ;; earth-pull: given a mass which is "height" meters above the ground (mean radius of the Earth), ;; return the force of gravity between it and the earth, in Newtons. ;; (define (earth-pull m height) (local [(define G 6.67e-11) ; Universal gravitational constant in SI units. (define earth-mass 5.97e24) ; in kg (define earth-radius 6380000) ; in m (define dist-from-center (+ earth-radius height))] (/ (* G earth-mass m) (sqr dist-from-center)))) "earth-pull test cases (1% accuaracy):" (> .01 (abs (/ (- 9.8 (earth-pull 1 0)) 9.8))) (> .01 (abs (/ (- 166.30 (earth-pull 17 23)) 166.30))) ;; sum: lon --> num ;; sums the list of numbers (define (sum lon) (cond [(empty? lon) 0] [(cons? lon) (local (;;helper: lon num --> num ;; returns the sum of the lon plus the sum-so-far.(define (helper lon sum-so-far) (cond [(empty? lon) sum-so-far] [(cons? lon) (helper (rest lon) (+ (first lon) sum-so-far))]))) (helper (rest lon) (first lon)))])) "sum test cases:" (= 0 (sum empty)) (= 15 (sum (list 1 2 3 4 5)))