<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On May 20, 2012, at 11:02 AM, Joe Gilray wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: 'Lucida Grande'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 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>You are changing a part of a recursive nest of methods, which is what inheritance is all about. </div><div><br></div><br><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: 'Lucida Grande'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 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><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><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> (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> (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> (inherit-field start)</div><div> (init sqrval)</div><div> (super-new [start (integer-sqrt sqrval)] [repeat (void)])</div><div> (inherit rational-rep show)</div><div> </div><div> (define repeatlst (build-repeatlst sqrval))</div><div> </div><div> ; build the repeating sequence (see <a href="http://en.wikipedia.org/wiki/Continued_fraction">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> ; perfect square:</div><div> '()] </div><div> [(= 1 denom)</div><div> ; found the end of the repeating sequence:</div><div> (reverse (cons (+ (integer-sqrt n) adder) result))] </div><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> (define/override (expand-repeat n)</div><div> (let lp ([c n] [rl '()] [stock repeatlst])</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></body></html>