[racket] strange loops
The body of a for loop is not in tail position wrt to the for loop
(you can see this in Check Syntax-- there is a gap in the tail arrows
between the let that binds 'next-z' and the call to 'next-z'.
Robby
On Tue, Mar 6, 2012 at 5:03 AM, Pierpaolo Bernardi <olopierpa at gmail.com> wrote:
> Hello,
>
> I was expecting the procedure 'fa' below to run in constant memory,
> as, in my understanding, It doesn't use any non-tail recursive loops,
> it does not build any data structure, and only performs arithmetic
> operations on small integers.
>
> But some of my assumptions must be wrong, as the calls to next-z
> accumulate and eventually fill the available memory.
>
> Can someone explain me where's my mistake?
>
> (A secondary note: the debugger shows the value of the squares vector
> as a 100 element vector, with no hint that the value is abbreviated.
> Pretty confusing, IMHO).
>
> Thanks.
>
> Pierpaolo
>
> ================
> #lang racket
>
> (define (perfect-square? n)
> (= n (sqr (integer-sqrt n))))
>
> (define (fa)
> (let ((squares (for/vector ((i (in-range 1 10000)))
> (* i i))))
> (let/ec return
> (let next-sum ((sum 3))
> (let ((limit-z (quotient sum 3)))
> (let next-z ((z 1))
> (if (= z limit-z)
> (next-sum (add1 sum))
> (for ((y+z (in-vector squares)))
> (let ((y (- y+z z)))
> (if (> (+ y z) sum)
> (next-sum (add1 sum))
> (let ((x (- sum z y)))
> (if (and (perfect-square? (+ x y))
> (perfect-square? (- x y))
> (perfect-square? (+ x z))
> (perfect-square? (- x z)))
> (return (list x y z sum))
> (next-z (add1 z))))))))))))))
>
> ================
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users