diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2023-07-07 15:10:11 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2023-07-07 15:10:11 +0200 |
| commit | 9cf86f51ee4dd078afadc45b8eb253bf28cf5241 (patch) | |
| tree | b26ade0ec17df582777bf9193ad8eae4f6c0e6ee /coding-exercises | |
| parent | a8a8e239968d45cc2539b6a55f4dcde04f5543fd (diff) | |
Diffstat (limited to 'coding-exercises')
| -rw-r--r-- | coding-exercises/3/3.5.2/53.rkt | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/coding-exercises/3/3.5.2/53.rkt b/coding-exercises/3/3.5.2/53.rkt index 776e619..275a990 100644 --- a/coding-exercises/3/3.5.2/53.rkt +++ b/coding-exercises/3/3.5.2/53.rkt @@ -13,7 +13,7 @@ (define (partial-sums s) (define me (stream-cons 0 (add-streams s me))) me) -(stream-ref (partial-sums integers) 4) +(display-stream-until (partial-sums integers) 4) (newline) (print "56") (newline) (define (merge s1 s2) @@ -40,4 +40,44 @@ (add-streams-counted (stream-cdr fibs) fibs)))) (stream-ref fibs 20) -(println additions) (newline) (println "Streams in racket are forced when car is called.") +(println additions) (newline) +(println "Without memoization the number of additions is proportional to the additions of the previous steps since each time all additions of previous steps are recalculated, which is a characteristic of exponential functions.") +(println "To show it is you could do a proof by counting additions at each step. Or use the fact that fibonacci has the phi^2 relation.") + +(newline) (print "58") (newline) +(define (expand num den radix) + (stream-cons + (quotient (* num radix) den) + (expand (remainder (* num radix) den) den radix))) +(stream-ref (expand 1 7 10) 3) +(stream-ref (expand 3 8 10) 2) + +(newline) (print "59") (newline) +(define (integrate-series s) (mul-streams s (stream-map / ones integers))) +(define exp-series (stream-cons 1 (integrate-series exp-series))) +(define cosine-series + (stream-cons 1 (integrate-series (scale-stream sine-series -1)))) +(define sine-series + (stream-cons 0 (integrate-series cosine-series))) + +(newline) (print "60") (newline) +;; This was my solution after doodling how to traverse the coefficients. +(define (mul-series s1 s2) + (stream-cons + (* (stream-car s1) (stream-car s2)) + (add-streams + (scale-stream (stream-cdr s2) (stream-car s1)) + (mul-series (stream-cdr s1) s2)))) +(display-stream-until (add-streams (mul-series sine-series sine-series) + (mul-series cosine-series cosine-series)) 8) + +;; This one is also interesting: http://community.schemewiki.org/?sicp-ex-3.60 +;; Instead of diagonal traversals in one direction, this one makes triangles. +(define (psum-mul-series s1 s2) + (stream-cons + (* (stream-car s1) (stream-car s2)) + (add-streams (add-streams (scale-stream (stream-cdr s1) (stream-car s2)) + (scale-stream (stream-cdr s2) (stream-car s1))) + (stream-cons 0 (psum-mul-series (stream-cdr s1) (stream-cdr s2)))))) +(display-stream-until (add-streams (psum-mul-series sine-series sine-series) + (psum-mul-series cosine-series cosine-series)) 8) |
