Oops, sorry! Yesterday evening I noticed that I messed it up, but was
too tired to dig into it.


On Wed, Feb 12, 2025 at 02:28:25PM -0800, Lindsay Lawrence wrote:
> Question: Without fully understanding the algorithm, it seems N is the next
> digit to return...

Right.

I posted the original 'while' version many years ago in Rosetta Code:

   https://rosettacode.org/wiki/Pi#PicoLisp

I translated it from the Haskell version, also without fully
understanding the algorithm :)


> But why write that as (swap 'N M)?  M is NIL if not defined but otherwise
> may have some other random value which, if set, does cause results to go
> astray.

Exactly! I noticed yesterday that 'lint' complained about the 'M'.

When I built the tco version yesterday, I experimended with both
versions, and somehow the 'M' code was lost :(

But very very strange that it seems to output the correct digits as long
as 'M' is NIL!


This is the correct original version:

   (de piDigit ()
      (job '((Q . 1) (R . 0) (S . 1) (K . 1) (N . 3) (L . 3))
         (while (>= (- (+ R (* 4 Q)) S) (* N S))
            (mapc set '(Q R S K N L)
               (list
                  (* Q K)
                  (* L (+ R (* 2 Q)))
                  (* S L)
                  (inc K)
                  (/ (+ (* Q (+ 2 (* 7 K))) (* R L)) (* S L))
                  (+ 2 L) ) ) )
         (prog1 N
            (let M (- (/ (* 10 (+ R (* 3 Q))) S) (* 10 N))
               (setq Q (* 10 Q)  R (* 10 (- R (* N S)))  N M) ) ) ) )

and this the tco version:

   (de piDigit ()
      (job '((Q . 1) (R . 0) (S . 1) (K . 1) (N . 3) (L . 3))
         (tco (Q R S K N L)
            (if (>= (- (+ R (* 4 Q)) S) (* N S))
               (tc
                  (* Q K)
                  (* L (+ R (* 2 Q)))
                  (* S L)
                  (inc K)
                  (/ (+ (* Q (+ 2 (* 7 K))) (* R L)) (* S L))
                  (+ 2 L) ) )
            (prog1 N
               (let M (- (/ (* 10 (+ R (* 3 Q))) S) (* 10 N))
                  (setq Q (* 10 Q)  R (* 10 (- R (* N S)))  N M) ) ) ) ) )

Sorry again for the confusion!

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to