Advertisement
AquaBlitz11

SICP lec 1 code

Feb 25th, 2023 (edited)
2,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.25 KB | None | 0 0
  1. #lang racket
  2.  
  3. ; In Racket, everything after a semicolon (;) is called a "comment." It won't be evaluated by Racket.
  4. ; Multiple lines commented are marked with #| ......... |#
  5. ; You'll find that some of the code below are commented out because they conflict with code later down the file.
  6.  
  7. ; Primitives and Means of Combination
  8.  
  9. (+ 9 10)
  10. ;19
  11.  
  12. (* (/ 4 3) 3.141592653 20 20 20)
  13. ;33510.321632
  14.  
  15. ; Practice: Write the following math expressions in prefix notation:
  16. ; (5 + 4 + (2 - (3 - (6 + 4/3)))) / (3(6-2)(2-7))
  17. ; Solution:
  18. (/ (+ 5
  19.       4
  20.       (- 2
  21.          (- 3
  22.             (+ 6
  23.                (/ 4 3)))))
  24.    (* 3
  25.       (- 6 2)
  26.       (- 2 7)))
  27. ;-23/90
  28.  
  29. ; Means of abstraction
  30. ; - Naming variables
  31.  
  32. (define pi 3.141592653)
  33. (define radius 20)
  34. (* (/ 4 3) pi radius radius radius)
  35. ;33510.321632
  36.  
  37. ; - Naming procedures
  38.  
  39. (define (sq x) (* x x))
  40. (sq (+ 5 7))
  41. ;144
  42. (sq (sq (sq (sq (sq 2)))))
  43. ;429497296
  44.  
  45. ; In-class exercise
  46. #|
  47. (define (sphere-volume radius)
  48.   ...)
  49. |#
  50. ; Solution:
  51. (define (sphere-volume radius)
  52.   (* (/ 4 3) pi radius radius radius))
  53. (sphere-volume 20)
  54. ;33510.321632
  55.  
  56. ; - Using conditionals
  57.  
  58. (define (abs x)
  59.   (cond [(> x 0) x]
  60.         [(= x 0) x]
  61.         [else (- x)]))
  62. (abs -3.14)
  63. ;3.14
  64. (abs 5.9)
  65. ;5.9
  66.  
  67. ; alternative way of defining abs
  68. #|
  69. (define (abs x)
  70.   (if (> x 0)
  71.       x
  72.       (- x)))
  73. |#
  74.  
  75. ; Exercise: Define compute-grade
  76. #|
  77. (define (compute-grade score)
  78.   ...)
  79. |#
  80.  
  81. #|
  82. ;A solution:
  83. (define (compute-grade score)
  84.   (cond [(and (>= score 90) (<= score 100)) "A"]
  85.         [(and (>= score 80) (< score 90)) "B"]
  86.         [(and (>= score 70) (< score 80)) "C"]
  87.         [else "F"]))
  88. |#
  89.  
  90. #|
  91. ;Another possible one:
  92. (define (compute-grade score)
  93.   (cond [(>= score 90) "A"]
  94.         [(<= 80 score 89) "B"]
  95.         [(<= 70 score 79) "C"]
  96.         [else "F"]))
  97. |#
  98.  
  99. ; The most succinct one:
  100. (define (compute-grade score)
  101.   (cond [(>= score 90) "A"]
  102.         [(>= score 80) "B"]
  103.         [(>= score 70) "C"]
  104.         [else "F"]))
  105.  
  106. (compute-grade 100)
  107. ;"A"
  108. (compute-grade 70)
  109. ;"C"
  110. (compute-grade 69)
  111. ;"F"
  112.  
  113. ;Other pathological cases:
  114. (compute-grade 0)
  115. ;"F"
  116. (compute-grade -25)
  117. ;"F"
  118. (compute-grade 1234)
  119. ;"A"
  120.  
  121. ; Computing square roots
  122.  
  123. #|
  124. (define (try guess x)
  125.   (if (good-enough? guess x)
  126.       guess
  127.       (try (improve guess x) x)))
  128.  
  129. (define (sqrt x) (try 1.0 x))
  130.  
  131. (define tolerance 0.000001)
  132.  
  133. (define (good-enough? guess x)
  134.   (< (distance (sq guess) x)
  135.      tolerance))
  136.  
  137. (define (distance a b)
  138.   (abs (- a b)))
  139.  
  140. (define (improve guess x)
  141.   (average guess (/ x guess)))
  142.  
  143. (define (average x y)
  144.   (/ (+ x y) 2))
  145.  
  146. (sqrt 2)
  147. ;1.4142135623746899
  148.  
  149. (sqrt 3)
  150. ;1.7320508100147274
  151. |#
  152.  
  153. ; Alternatively, we could put all of this in a block structure.
  154.  
  155.  
  156. (define (sqrt x)
  157.   (define tolerance 0.000001)
  158.   (define (try guess)
  159.     (if (good-enough? guess)
  160.         guess
  161.         (try (improve guess))))
  162.   (define (good-enough? guess)
  163.     (< (distance (sq guess) x) tolerance))
  164.   (define (improve guess)
  165.     (average guess (/ x guess)))
  166.   (try 1.0))
  167.  
  168. ; It probably makes more sense to put those functions outside than inside sqrt.
  169. (define (distance a b)
  170.   (abs (- a b)))
  171. (define (average x y)
  172.   (/ (+ x y) 2))
  173.  
  174. (sqrt 2)
  175. ;1.4142135623746899
  176.  
  177. (sqrt 3)
  178. ;1.7320508100147274
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement