thanks. Very subtle difference... definitely not a good idea to try
to double an existing variable as an accumulator too...
I reworked it to:
(define (pi4 accuracy)
(let helper ((k 0) (r 0))
(let ((this (* (/ (expt -1 k) (expt 4 k))
(+ (/ 2 (+ (* 4 k) 1))
If you systematically apply cps and then accumulator transformations, you get
this:
(define (pi3 accuracy)
(let helper ((k 0))
(let ((this (formula k)))
(if (< (abs this) accuracy)
this
(+ this (helper (+ k 1)))
(pi3 .1)
(define (pi3-cps accuracy)
(let he
Hello, this is not really racket-related, for I get same issue with
larceny, for instance. I saw a function at reddit and tried reworking
it into tail-recursion, but it baffles me why last version shouldn't
work.
; original function: works
(define pi
(lambda (accuracy)
(letrec ((helper
3 matches
Mail list logo