<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) &nbsp;Is what I've done an abomination? &nbsp;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.&nbsp;</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) &nbsp;To make the code below work, I had to make expand-repeat public as I couldn't override a private function. &nbsp;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.&nbsp;</div><div><br></div><div><div>#lang racket</div><div><br></div><div>(require racket/class)</div><div><br></div><div>(provide</div><div>&nbsp;gencontfrac%&nbsp;</div><div>&nbsp;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&nbsp;</div><div>; and acts as a base class for continued fractions of other sorts</div><div>(define gencontfrac%&nbsp;</div><div>&nbsp; (class object%</div><div>&nbsp; &nbsp; (super-new)</div><div>&nbsp; &nbsp; (init-field start repeat)</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; ; create a n-long list of repeat digits in reverse order,&nbsp;</div><div>&nbsp; &nbsp; ; which facilitates the creation of a rational</div><div>&nbsp; &nbsp; (define/public (expand-repeat n)</div><div>&nbsp; &nbsp; &nbsp; (let lp ([c n] [rl '()])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; (if (zero? c) rl (lp (sub1 c) (cons (repeat) rl)))))</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; ; create a rational representation using the passed number of repeat digits</div><div>&nbsp; &nbsp; (define/public (rational-rep n)</div><div>&nbsp; &nbsp; &nbsp; (define rdiglst (expand-repeat n))</div><div>&nbsp; &nbsp; &nbsp; (let lp ([rdigs (rest rdiglst)] [res (first rdiglst)])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; (if (empty? rdigs) (+ start (/ 1 res)) (lp (rest rdigs) (+ (first rdigs) (/ 1 res))))))</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; ; display the continued fraction</div><div>&nbsp; &nbsp; (define/public (show)</div><div>&nbsp; &nbsp; &nbsp; (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%&nbsp;</div><div>&nbsp; (class gencontfrac%</div><div>&nbsp; &nbsp; (inherit-field start)</div><div>&nbsp; &nbsp; (init sqrval)</div><div>&nbsp; &nbsp; (super-new [start (integer-sqrt sqrval)] [repeat (void)])</div><div>&nbsp; &nbsp; (inherit rational-rep show)</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; (define repeatlst (build-repeatlst sqrval))</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; ; build the repeating sequence (see <a href="http://en.wikipedia.org/wiki/Continued_fraction">http://en.wikipedia.org/wiki/Continued_fraction</a>)</div><div>&nbsp; &nbsp; (define/private (build-repeatlst n)</div><div>&nbsp; &nbsp; &nbsp; (define start (integer-sqrt n))</div><div>&nbsp; &nbsp; &nbsp; (let loop ([adder start] [denom (- n (sqr start))] [result '()])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; (cond</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(zero? denom)&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; perfect square:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'()]&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(= 1 denom)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; found the end of the repeating sequence:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(reverse (cons (+ (integer-sqrt n) adder) result))]&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(let* ([nextval (quotient (+ adder start) denom)] [nextadd (- (* nextval denom) adder)])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(loop nextadd (quotient (- n (sqr nextadd)) denom) (cons nextval result)))])))</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; (define/override (expand-repeat n)</div><div>&nbsp; &nbsp; &nbsp; (let lp ([c n] [rl '()] [stock repeatlst])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; (cond</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(zero? c) rl]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(define r (rest stock))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(lp (sub1 c) (cons (first stock) rl) (if (empty? r) repeatlst r))])))</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; ; accessor</div><div>&nbsp; &nbsp; (define/public (get-repeatlst) repeatlst)))</div><div><br></div></div></body></html>