Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. (define (square x) (* x x))
  2. (define (sum-of-squares x y)
  3. (+ (square x) (square y)))
  4. (define (distance-from-origin x y)
  5. (sqrt (sum-of-squares x y)))
  6.  
  7. (define (abs x)
  8. (cond ((< x 0) (- x))
  9. (else x)))
  10.  
  11. (define (sum-of-sq-of-two-larger-numbers a b c)
  12. (if (< a b)
  13. (if (< a c)
  14. (sum-of-squares b c)
  15. (sum-of-squares a b))
  16. (if (< b c)
  17. (sum-of-squares a c)
  18. (sum-of-squares a b))))
  19.  
  20. (define (a-plus-abs-b a b)
  21. ((if (> b 0) + -) a b))
  22.  
  23. ;; recursive definition of factorial
  24. ;; a simple example of linear recursive process.
  25. (define (factorial n)
  26. (if (zero? n)
  27. 1
  28. (* n (factorial (- n 1)))))
  29.  
  30. ;; linear iterative process . tail recusive procedure.
  31. (define (factorial-iter n)
  32. (define (fact-iter accum count num)
  33. (if (= count num)
  34. accum
  35. (fact-iter (* accum (+ count 1))
  36. (+ count 1)
  37. num)))
  38. (fact-iter 1 1 n))
  39.  
  40. (define (make-serial-number-generator)
  41. (let ((current-number 0))
  42. (lambda ()
  43. (set! current-number (+ current-number 1))
  44. current-number)))
  45.  
  46. (define (sqrt-iter guess x)
  47. (if (good-enough? guess x)
  48. guess
  49. (sqrt-iter (improve guess x)
  50. x)))
  51.  
  52. (define (improve guess x)
  53. (average guess (/ x guess)))
  54.  
  55. (define (average x y)
  56. (/ (+ x y) 2))
  57.  
  58. (define (good-enough? guess x)
  59. (< (abs (- (square guess) x)) 0.001))
  60.  
  61. (define (sqrt x)
  62. (sqrt-iter 1.0 x))
  63.  
  64. ;; exercise 1.10 ackerman function.
  65.  
  66. (define (A x y)
  67. (cond ((= y 0) 0)
  68. ((= x 0) (* 2 y))
  69. ((= y 1) 2)
  70. (else (A (- x 1)
  71. (A x (- y 1))))))
  72. (define (f n) (A 0 n));; computes 2n
  73. (define (g n) (A 1 n));; computes 2^n
  74. (define (h n) (A 2 n))
  75. ;; gives exponential recursion func i.e., (h n)= 2^(h (- n 1))
  76. ;; mathematically concise definition would be 2^2^2...^2 n-1 2's
  77.  
  78. ;;; Tree recursion.
  79. (define (fibonacci n)
  80. (cond ((= n 0) 1)
  81. ((= n 1) 1)
  82. (else (+ (fibonacci (- n 1))
  83. (fibonacci (- n 2))))))
  84.  
  85. ;; iterative process with 3 state variables.
  86. (define (fibonacci-iter n)
  87. (define (fib-iter a b count)
  88. (if (= count 0)
  89. b
  90. (fib-iter (+ a b) a (- count 1))))
  91. (fib-iter 1 0 n))
  92.  
  93. ;;; Counting change.
  94. ;; write a procedure to compute the number of ways to change
  95. ;; any given amount of money.
  96.  
  97. (define (count-change amount denominations)
  98. (cond ((null? denominations) 0)
  99. ((zero? amount) 1)
  100. ((< amount 0) 0)
  101. (else (+ (count-change amount (cdr denominations)); without the first denomination.
  102. (count-change (- amount (car denominations)) denominations) ; with one first denomination.
  103. ))))
  104.  
  105. ;; TODO: write the above function as an iterative process.
  106.  
  107.  
  108. ;;; exercise 1.11 f(n) = f(n-1) + 2*f(n-2) + 3*f(n-3)
  109. ;; write a procedure to compute f(n) by an iterative process
  110. ;; tree recusive procedure
  111. (define (f-1-11 n)
  112. (cond ((= n 1) 1)
  113. ((= n 2) 2)
  114. ((= n 3) 3)
  115. (else (+ (f-1-11 (- n 1))
  116. (* 2 (f-1-11 (- n 2)))
  117. (* 3 (f-1-11 (- n 3)))))))
  118. ;; iterative procedure
  119. (define (f-1-11-iter n)
  120. (define (f-iter a b c d count)
  121. (if (zero? count)
  122. a
  123. (f-iter (+ a (* 2 b) (* 3 c)) a b c (- count 1))))
  124. (cond ((= n 1) 1)
  125. ((= n 2) 2)
  126. ((= n 3) 3)
  127. (else (f-iter 10 3 2 1 (- n 4)))))
  128.  
  129. ;;; exercise 1.12 pascal's triangle
  130. ;; write a procedure that computes the elements of pascal's triangle by
  131. ;; means of a recursive process.
  132. (define (pascals-triangle n)
  133. (define (row n)
  134. (if (= n 1)
  135. (quote (1))
  136. (let ((prev (row (- n 1))))
  137. (zip-dis prev (cons 0 prev) '()))))
  138. (define (zip-dis l r accum)
  139. (cond ((null? l) (catenate r accum))
  140. ((null? r) (catenate l accum))
  141. (else (zip-dis (cdr l)
  142. (cdr r)
  143. (cons (+ (car l) (car r)) accum)))))
  144. (define (catenate l r)
  145. (if (null? l)
  146. r
  147. (catenate (cdr l) (cons (car l) r))))
  148. (row n))
  149.  
  150. ;;; 1.3 Orders of growth.
  151. ;; TODO: what's wrong with this function?
  152. (define (sine x)
  153. (define (f1 a)
  154. (- (* 3 x)
  155. (* 4 x x x)))
  156. (define (abs a)
  157. (if (< a 0)
  158. (- a)
  159. a))
  160. (if (< (abs x) 0.1)
  161. x
  162. (f1 (sine (/ x 3.0)))))
  163.  
  164. ;;; 1.4 exponentiation
  165. (define (my-expt x y)
  166. (if (= y 0)
  167. 1
  168. (* x (my-expt x (- y 1)))))
  169.  
  170. ;; iteration procedure defined outside for better ,trace
  171. (define (my-expt-iter-a x y accum)
  172. (if (= y 0)
  173. accum
  174. (my-expt-iter-a x (- y 1) (* accum x))))
  175. (define (my-expt-iter x y)
  176. (my-expt-iter-a x y 1))
  177.  
  178. (define (fast-expt x y)
  179. (cond ((= y 0) 1)
  180. ((even? y) (square (fast-expt x (/ y 2))))
  181. ((odd? y) (* x (square (fast-expt x (/ (- y 1) 2)))))
  182. (else (error "wrong exponent"))))
  183. ;; exercise 1.16
  184. ;; design a procedure that evolves an iterative exponentiation process using
  185. ;; successive squaring and uses logarithmic number of steps, similar to fast-expt
  186. (define (fast-expt-iter a b accum)
  187. (cond ((= b 0) accum)
  188. ((even? b) (fast-expt-iter (* a a) (/ b 2) accum)) ; finding an invariant in state vars
  189. (else (fast-expt-iter a (- b 1) (* a accum)))))
  190. (define (fast-expt-i x y)
  191. (fast-expt-iter x y 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement