-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathch3support.scm
50 lines (39 loc) · 1.52 KB
/
ch3support.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;; CODE TO SUPPORT CHAPTER 3 OF STRUCTURE AND INTERPRETATION OF
;;; COMPUTER PROGRAMS
;;; NB. This code is *not* from the book
;;; In addition to code supplied here
;;;**For 3.4, might want parallel-execute as implemented for MIT Scheme
;;;**For 3.5, need stream special forms, which are not in Standard Scheme
;;For Section 3.1.2 -- written as suggested in footnote,
;; though the values of a, b, m may not be very "appropriately chosen"
(define (rand-update x)
(let ((a 27) (b 26) (m 127))
(modulo (+ (* a x) b) m)))
;;For Section 3.3.4, used by and-gate
;;Note: logical-and should test for valid signals, as logical-not does
(define (logical-and x y)
(if (and (= x 1) (= y 1))
1
0))
;;For Section 3.5 -- useful for looking at finite amounts of infinite streams
;;Print the first n elements of the stream s.
;;One version prints on one line, one on separate lines
(define (print-n s n)
(if (> n 0)
(begin (display (stream-car s))
(display ",")
(print-n (stream-cdr s) (- n 1)))))
(define (print-n s n)
(if (> n 0)
(begin (newline)
(display (stream-car s))
(print-n (stream-cdr s) (- n 1)))))
;;For Section 3.5.2, to check power series (exercises 3.59-3.62)
;;Evaluate and accumulate n terms of the series s at the given x
;;Uses horner-eval from ex 2.34
(define (eval-power-series s x n)
(horner-eval x (first-n-of-series s n)))
(define (first-n-of-series s n)
(if (= n 0)
'()
(cons (stream-car s) (first-n-of-series (stream-cdr s) (- n 1)))))