I'm trying to learn to use classes in Racket. I did some successful simple experiments then moved on to inheritance. When I try to use the code below, the following code:<div><br></div><div>(new sqrtcontfrac% [sqrval i]) leads to the message: sqr: expected argument of type <number>; given #<undefined></div>
<div><br></div><div>Why isn't start being inherited properly? Any help is appreciated!</div><div><br></div><div>Thanks,</div><div>-joe</div><div><br></div><div>Here is the class code:</div><div><br></div><div><div>(module contfrac racket</div>
<div> (require racket/class)</div><div> (provide gencontfrac%)</div><div> (provide sqrtcontfrac%)</div><div> </div><div> ; class that represents the continued fraction of a general irrational number 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) <b>; here start is defined</b></div><div> (init-field repeat)</div><div> ; create a n-long list of repeat digits (in reverse order - which makes creation of a rational easier)</div>
<div> (define/private (expand-repeat n)</div><div> (let lp ([c n] [rl '()])</div><div> (if (zero? c) rl (lp (sub1 c) (cons (repeat) rl)))))</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> ; display the continued fraction</div><div> (define/public (show)</div><div> (printf "[~a; ~a]~n" start repeat))</div><div> ))</div><div> </div><div> ; define a class that represents the continued fraction of a square root</div>
<div> (define sqrtcontfrac% </div><div> (class gencontfrac%</div><div> (init sqrval)</div><div> (super-new [start (integer-sqrt sqrval)] [repeat (build-repeat sqrval)])</div><div> (inherit rational-rep show)</div>
<div> (inherit-field start repeat)</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-repeat n)</div>
<div> (let loop ([adder start] [denom (- n (sqr start))] [result '()]) <b>; this is where the error is occurring </b></div><div> (cond [(zero? denom) '()] ; perfect square</div><div> [(= 1 denom) (reverse (cons (+ start adder) result))] ; found the end of the repeating sequence</div>
<div> [else </div><div> (let* ([nextval (quotient (+ adder (integer-sqrt n)) denom)] [nextadd (- (* nextval denom) adder)])</div><div> (loop nextadd (quotient (- n (sqr nextadd)) denom) (cons nextval result)))])))</div>
<div> ; create a n-long list of repeat digits (in reverse order - which makes creation of a rational easier)</div><div> (define/private (expand-repeat n)</div><div> (let lp ([c n] [rl '()] [stock repeat])</div>
<div> (if (zero? c) rl (lp (sub1 c) (cons (first stock) rl) (if (empty? (rest stock)) repeat (rest stock))))))</div><div> ))</div><div> )</div></div>