Hi Matthias,<div><br></div><div>Thanks for the lesson and the reformatting too. I had thought that the "(module... " format was needed... it's nice to get back that indent.</div><div><br></div><div>In case you're intestered, here some of my uses of these continued fraction classes:</div>
<div><br></div><div>First a use of gencontfrac%</div><div><br></div><div><div><font face="courier new, monospace">; repeat procedure for continued fraction representing e (Euler's constant)</font></div><div><font face="courier new, monospace">(define e-repeat-proc</font></div>
<div><font face="courier new, monospace"> (generator ()</font></div><div><font face="courier new, monospace"> (let loop ([count 3])</font></div><div><font face="courier new, monospace"> (if (= 1 (remainder count 3))</font></div>
<div><font face="courier new, monospace"> (yield (* 2 (quotient count 3)))</font></div><div><font face="courier new, monospace"> (yield 1))</font></div><div><font face="courier new, monospace"> (loop (add1 count)))))</font></div>
<div><font face="courier new, monospace"> </font></div><div><font face="courier new, monospace">(define (euler65)</font></div><div><font face="courier new, monospace"> (define ecf (new gencontfrac% [start 2] [repeat e-repeat-proc]))</font></div>
<div><font face="courier new, monospace"> (define frac (send ecf rational-rep 99))</font></div><div><font face="courier new, monospace"> (apply + (number->list (numerator frac))))</font></div><div><font face="courier new, monospace">(check-expect (euler65) 272)</font></div>
<div><br></div><div>Second, a use of sqrtcontfrac%</div><div><br></div><div><div><font face="courier new, monospace">(define (euler57)</font></div><div><font face="courier new, monospace"> (define sqrt2cf (new sqrtcontfrac% [sqrval 2]))</font></div>
<div><font face="courier new, monospace"> (for/sum ([i (in-range 1 1001)]) </font></div><div><font face="courier new, monospace"> (define frac (send sqrt2cf rational-rep i))</font></div><div><font face="courier new, monospace"> (if (> (order-of-magnitude (numerator frac)) (order-of-magnitude (denominator frac))) 1 0)))</font></div>
<div><font face="courier new, monospace">(check-expect (euler57) 153)</font></div></div><div><br></div><div>Thanks again,</div><div>-Joe</div><div><br></div><br><div class="gmail_quote">On Sun, May 20, 2012 at 11:40 AM, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div class="im"><div>On May 20, 2012, at 11:02 AM, Joe Gilray wrote:</div><br>
<blockquote type="cite"><span style="border-collapse:separate;font-family:'Lucida Grande';font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium"><div>
1) Is what I've done an abomination? Am I simply abusing inheritance?</div></span></blockquote><div><br></div></div><div>You are changing a part of a recursive nest of methods, which is what inheritance is all about. </div>
<div class="im"><div><br></div><br><blockquote type="cite"><span style="border-collapse:separate;font-family:'Lucida Grande';font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium"><div>
2) To make the code below work, I had to make expand-repeat public as I couldn't override a private function. Is there another way (something like "protected" in C++)?</div></span></blockquote></div></div>
<br><div>(define-local-member-name expand-repeat)</div><div><br></div><div>Don't export expand-repeat and nobody can run this method from outside. </div><div><br></div><div><div>#lang racket</div><div><br></div><div>(require racket/class)</div>
<div><br></div><div>(provide</div><div> gencontfrac% </div><div> sqrtcontfrac%)</div><div><br></div><div>;; ---------------------------------------------------------------------------------------------------</div><div><br>
</div><div>(define-local-member-name expand-repeat)</div><div class="im"><div><br></div><div>; class that represents the continued fraction of a general irrational number </div><div>; and acts as a base class for continued fractions of other sorts</div>
<div>(define gencontfrac% </div><div> (class object%</div><div> (super-new)</div></div><div> (init-field start repeat)</div><div> </div><div> ; create a n-long list of repeat digits in reverse order, </div><div>
; which facilitates the creation of a rational</div><div class="im"><div> (define/public (expand-repeat n)</div><div> (let lp ([c n] [rl '()])</div><div> (if (zero? c) rl (lp (sub1 c) (cons (repeat) rl)))))</div>
<div> </div><div> ; create a rational representation using the passed number of repeat digits</div><div> (define/public (rational-rep n)</div><div> (define rdiglst (expand-repeat n))</div><div> (let lp ([rdigs (rest rdiglst)] [res (first rdiglst)])</div>
<div> (if (empty? rdigs) (+ start (/ 1 res)) (lp (rest rdigs) (+ (first rdigs) (/ 1 res))))))</div><div> </div><div> ; display the continued fraction</div><div> (define/public (show)</div><div> (printf "[~a; ~a]~n" start repeat))))</div>
<div><br></div><div>; define a class that represents the continued fraction of a square root</div><div>(define sqrtcontfrac% </div><div> (class gencontfrac%</div></div><div> (inherit-field start)</div><div> (init sqrval)</div>
<div class="im"><div> (super-new [start (integer-sqrt sqrval)] [repeat (void)])</div><div> (inherit rational-rep show)</div><div> </div></div><div> (define repeatlst (build-repeatlst sqrval))</div><div class="im">
<div> </div><div> ; build the repeating sequence (see <a href="http://en.wikipedia.org/wiki/Continued_fraction" target="_blank">http://en.wikipedia.org/wiki/Continued_fraction</a>)</div><div> (define/private (build-repeatlst n)</div>
<div> (define start (integer-sqrt n))</div><div> (let loop ([adder start] [denom (- n (sqr start))] [result '()])</div><div> (cond</div><div> [(zero? denom) </div></div><div> ; perfect square:</div>
<div> '()] </div><div> [(= 1 denom)</div><div> ; found the end of the repeating sequence:</div><div class="im"><div> (reverse (cons (+ (integer-sqrt n) adder) result))] </div></div>
<div class="im"><div> [else </div><div> (let* ([nextval (quotient (+ adder start) denom)] [nextadd (- (* nextval denom) adder)])</div><div> (loop nextadd (quotient (- n (sqr nextadd)) denom) (cons nextval result)))])))</div>
<div> </div></div><div class="im"><div> (define/override (expand-repeat n)</div><div> (let lp ([c n] [rl '()] [stock repeatlst])</div></div><div> (cond</div><div> [(zero? c) rl]</div><div> [else </div>
<div> (define r (rest stock))</div><div> (lp (sub1 c) (cons (first stock) rl) (if (empty? r) repeatlst r))])))</div><div> </div><div> ; accessor</div><div> (define/public (get-repeatlst) repeatlst)))</div>
<div><br></div></div></div></blockquote></div><br></div>