Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- ; In Racket, everything after a semicolon (;) is called a "comment." It won't be evaluated by Racket.
- ; Multiple lines commented are marked with #| ......... |#
- ; You'll find that some of the code below are commented out because they conflict with code later down the file.
- ; Primitives and Means of Combination
- (+ 9 10)
- ;19
- (* (/ 4 3) 3.141592653 20 20 20)
- ;33510.321632
- ; Practice: Write the following math expressions in prefix notation:
- ; (5 + 4 + (2 - (3 - (6 + 4/3)))) / (3(6-2)(2-7))
- ; Solution:
- (/ (+ 5
- 4
- (- 2
- (- 3
- (+ 6
- (/ 4 3)))))
- (* 3
- (- 6 2)
- (- 2 7)))
- ;-23/90
- ; Means of abstraction
- ; - Naming variables
- (define pi 3.141592653)
- (define radius 20)
- (* (/ 4 3) pi radius radius radius)
- ;33510.321632
- ; - Naming procedures
- (define (sq x) (* x x))
- (sq (+ 5 7))
- ;144
- (sq (sq (sq (sq (sq 2)))))
- ;429497296
- ; In-class exercise
- #|
- (define (sphere-volume radius)
- ...)
- |#
- ; Solution:
- (define (sphere-volume radius)
- (* (/ 4 3) pi radius radius radius))
- (sphere-volume 20)
- ;33510.321632
- ; - Using conditionals
- (define (abs x)
- (cond [(> x 0) x]
- [(= x 0) x]
- [else (- x)]))
- (abs -3.14)
- ;3.14
- (abs 5.9)
- ;5.9
- ; alternative way of defining abs
- #|
- (define (abs x)
- (if (> x 0)
- x
- (- x)))
- |#
- ; Exercise: Define compute-grade
- #|
- (define (compute-grade score)
- ...)
- |#
- #|
- ;A solution:
- (define (compute-grade score)
- (cond [(and (>= score 90) (<= score 100)) "A"]
- [(and (>= score 80) (< score 90)) "B"]
- [(and (>= score 70) (< score 80)) "C"]
- [else "F"]))
- |#
- #|
- ;Another possible one:
- (define (compute-grade score)
- (cond [(>= score 90) "A"]
- [(<= 80 score 89) "B"]
- [(<= 70 score 79) "C"]
- [else "F"]))
- |#
- ; The most succinct one:
- (define (compute-grade score)
- (cond [(>= score 90) "A"]
- [(>= score 80) "B"]
- [(>= score 70) "C"]
- [else "F"]))
- (compute-grade 100)
- ;"A"
- (compute-grade 70)
- ;"C"
- (compute-grade 69)
- ;"F"
- ;Other pathological cases:
- (compute-grade 0)
- ;"F"
- (compute-grade -25)
- ;"F"
- (compute-grade 1234)
- ;"A"
- ; Computing square roots
- #|
- (define (try guess x)
- (if (good-enough? guess x)
- guess
- (try (improve guess x) x)))
- (define (sqrt x) (try 1.0 x))
- (define tolerance 0.000001)
- (define (good-enough? guess x)
- (< (distance (sq guess) x)
- tolerance))
- (define (distance a b)
- (abs (- a b)))
- (define (improve guess x)
- (average guess (/ x guess)))
- (define (average x y)
- (/ (+ x y) 2))
- (sqrt 2)
- ;1.4142135623746899
- (sqrt 3)
- ;1.7320508100147274
- |#
- ; Alternatively, we could put all of this in a block structure.
- (define (sqrt x)
- (define tolerance 0.000001)
- (define (try guess)
- (if (good-enough? guess)
- guess
- (try (improve guess))))
- (define (good-enough? guess)
- (< (distance (sq guess) x) tolerance))
- (define (improve guess)
- (average guess (/ x guess)))
- (try 1.0))
- ; It probably makes more sense to put those functions outside than inside sqrt.
- (define (distance a b)
- (abs (- a b)))
- (define (average x y)
- (/ (+ x y) 2))
- (sqrt 2)
- ;1.4142135623746899
- (sqrt 3)
- ;1.7320508100147274
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement