Hi, I created the following code from some scheme I found on ProjectEuler.net (problem #9):<div><br><div><div>(define (right-triangle? a b c)</div><div> (= (+ (* a a) (* b b)) (* c c)))</div></div><div><div><br></div><div>
(define (pythagorean-triple n)</div><div> (let loop-a ([a 1])</div><div> (let loop-b ([b (add1 a)])</div><div> (let ([c (- n a b)])</div><div> (if (<= c b) </div><div> (loop-a (add1 a))</div>
<div> (if (right-triangle? a b c)</div><div> (list a b c)</div><div> (loop-b (add1 b)))))))</div></div><div><br></div><div>Is the following a better way to write this?</div><div><br>
</div><div><div>(define (pythagorean-triple n)</div><div> (let loop-ab ([a 1] [b 2])</div><div> (let ([c (- n a b)])</div><div> (if (<= c b) </div><div> (loop-ab (add1 a) (+ a 2))</div><div> (if (right-triangle? a b c)</div>
<div> (list a b c)</div><div> (loop-ab a (add1 b)))))))</div></div><div><br></div><div>I wish I could do "(let* loop-ab ([a 1] [b (add1 a)])...", but that is not allowed. In any event, this second version of pythagorean-triple feels a little better than the original to me. Of course taking the next step, feels like going too far as it makes the relationship between n, c, a & b harder to see:</div>
<div><br></div><div><div>(define (pythagorean-triple n)</div><div> (let loop-abc ([a 1] [b 2] [c (- n 3)])</div><div> (if (<= c b) </div><div> (loop-abc (add1 a) (+ a 2) (- n (+ 3 (* a 2))))</div><div> (if (right-triangle? a b c)</div>
<div> (list a b c)</div><div> (loop-abc a (add1 b) (- n (+ a (add1 b))))))))</div></div><div><br></div><div>Thoughts?</div><div><br></div><div>-joe</div><div><br></div><div><br></div></div>