Chris Pollett>Old Classes>PIC 15, Fall 1999>Hw5>HW5 Solution

Fall 1999 PIC 15 HW5 Solutions Page


;3.58
; We are asked what stream the following piece of code computes:

(define (expand num den radix)
   (cons-stream
     (quotient (* num radix) den)
     (expand (remainder (*num radix) den) den radix)))
)

; The answer is that it computes the base radix expansion of the
; fraction num/den. For example, (expand 1 3 2) would
; be the base 2 expansion of 1/3 which is 0.1010101...
; and the stream would be (1 0 1 0 1 ...)
;
; (expand 1 7 10) is (1 4 2 8 5 7 ...)
; and
; (expand 3 8 10) is (6 7 5 0 0 0...)

;3.59a We are asked to write a function integrate-series
; which takes a stream viewed as a power-series and integrates it.


;Name: integrate-series
;=====
;Purpose: 
;========
;Takes as inputs a stream viewed as a power series and outputs
;a stream representing the integral of this series.
;
;Known Bugs:
;===========
; none
(define (integrate-series s)
   (pointwise-div s integers)
)

;the next two definitions are from the book.
(define (integers-starting-from n)
    (cons-stream n (integers-starting-from (+ n 1)))
) 

(define integers (integers-starting-from 1))

;Name: pointwise-div
;=====
;Purpose: 
;========
;Takes two streams and produces the stream which results by dividing them
; point by point.
;Example s1 := (1 2 3 4 5 ...)
;        s2 := (2 3 4 5 6 ...)
; (pointwise-div s1 s2) is (1/2 2/3 3/4 4/5 5/6...)
;
;Known Bugs:
;===========
; none

(define (pointwise-div s1 s2)
    (cons-stream 
         (/ (stream-car s1) (stream-car s2)) 
         (pointwise-div (stream-cdr s1) (stream-cdr s2))
    )
)

;3.59b We are asked to define a stream for the sine series and cosine series
; Ans:

;
;Name: neg-stream
;=====
;Purpose: 
;========
; takes a stream s and returns stream where each value has been negated.
;
;Known Bugs:
;===========
; none

(define (neg-stream s) 
   (cons-stream (- (stream-car s)) (neg-stream (stream-cdr s))))

(define cosine-series (cons-stream 1
                        (neg-stream (integrate-series sine-series))))

(define sine-series  (cons-stream 0
                       (integrate-series cosine-series)))
 

;Below is the written homework program...

%
% numbers
%
% a program for printing out numbers up to a billion minus one.
% I went with class consensus and 101 is one hundred one.
%

numbers :- repeat,
        display('Enter a number followed by a period and return:'),nl,
        read(X),
        phrase(num(X),Out),nl,nl,
        display('In English your number was: '),
        print_list(Out),display('.'),
        nl,nl, 
	display('Would you like to do another number?(type y or n, a period, and return)'),
        read(Y),
        (Y='n' ; Y = 'N').

print_list([]).
print_list( [X|Y] ) :- display(X), display(' '), print_list(Y). 

num(0) --> [zero],!.
num(N) --> x9(N),!. % should only find one solution for a number
                    %this is what's called a red-cut since it actually deletes
                    %solutions.

x(N) --> digit(N).

xx(N) --> x(N).
xx(N) --> teens(N).
xx(N) --> tens(T), digit(N1), {N is T+N1}.

xxx(N) --> xx(N).
xxx(N) --> digit(H), [hundred], xx(N1), {N is H*100+N1}.

x6(N) -->xxx(N).
x6(N) -->xxx(Th), [thousand], xxx(N1), {N is Th*1000+N1}.

x9(N) -->x6(N).
x9(N) -->xxx(M), [million], x6(N1), {N is M*1000000+N1}.


digit(0) --> [].
digit(1) --> [one].
digit(2) --> [two].
digit(3) --> [three].
digit(4) --> [four].
digit(5) --> [five].
digit(6) --> [six].
digit(7) --> [seven].
digit(8) --> [eight].
digit(9) --> [nine].

teens(10) --> [ten].
teens(11) --> [eleven].
teens(12) --> [twelve].
teens(13) --> [thirteen].
teens(14) --> [fourteen].
teens(15) --> [fifteen].
teens(16) --> [sixteen].
teens(17) --> [seventeen].
teens(18) --> [eighteen].
teens(19) --> [nineteen].

tens(20) --> [twenty].
tens(30) --> [thirty].
tens(40) --> [forty].
tens(50) --> [fifty].
tens(60) --> [sixty].
tens(70) --> [seventy].
tens(80) --> [eighty].
tens(90) --> [ninety].