Re: Unexpected behavior
Thanks Alex! This makes sense to me now... (de Fibonacci (N) (let (F (list 2 1 1)) (cond ((= N 0) 0) ((= N 1) (caddr F)) ((= N 2) (cadr F)) (T (do (- N 3) (rot F) (set F (+ (cadr F) (caddr F))) ) (car F) ) ) ) ) /Lindsay On Sat, Mar 11, 2017 at 11:35 AM, Alexander Burger wrote: > On Sat, Mar 11, 2017 at 11:20:21AM -0800, Lindsay John Lawrence wrote: > > Nm... After tinkering in the debugger.. using 'pp etc.. > > I get it. I am still getting the (= code data) in picolisp. > > Cool! Our mails overlapped! :) > > ♪♫ Alex > -- > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe >
Re: Unexpected behavior
Or strictly speaking... (de Fibonacci (N) (let (Fib '(2 1 1)) (set Fib 2 (cdr Fib) 1 (cddr Fib) 1) (cond ((= N 0) 0) ((= N 1) (caddr Fib)) ((= N 2) (cadr Fib)) (T (do (- N 3) (rot Fib) (set Fib (+ (cadr Fib) (caddr Fib))) ) (car Fib)) ) ) ) /Lindsay On Sat, Mar 11, 2017 at 11:20 AM, Lindsay John Lawrence < lawrence.lindsayj...@gmail.com> wrote: > Nm... After tinkering in the debugger.. using 'pp etc.. > I get it. I am still getting the (= code data) in picolisp. > > This gives the results I wanted. > > (de Fibonacci (N) >(let (Fib '(1 1 0) R 0) > (do N > (rot Fib) > (set Fib (+ (cadr Fib) (caddr Fib))) > ) > (setq R (car Fib)) > (set Fib 1 (cdr Fib) 1 (cddr Fib) 0) > R > ) ) > > Now I can write... > > : (Fibonacci 1) > -> 2 > : (Fibonacci 10) > -> 144 > : (bench (nil (Fibonacci 10))) > 3.388 sec > : (length (chop (Fibonacci 10))) > -> 20899 digits! > > Picolisp bignums are awesome! > > /Lindsay > > >
Re: Unexpected behavior
Hi Lindsay, > (de Fibonacci (N) >(let (Fib '(1 1 0)) > (do N > (rot Fib) > (set Fib (+ (cadr Fib) (caddr Fib))) ) >(car Fib) ) ) > > The results are something like a 'co routine or 'job... in that Fib as a > memory between calls and it is not re-initialized with the 'let as I > expected. This is because 'rot' is a destructive operation. The list (1 1 0), i.e. these three *cells*, are a constant in the body of the function (please 'F' -> 'f' ;) You can see this if you do (pp 'fibonacci) after you executed it. As in all such cases of destructive operations, the right way is to use a fresh (newly built) list: (de fibonacci (N) (let Fib (list 1 1 0) # builds a new list (do N ... ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Unexpected behavior
On Sat, Mar 11, 2017 at 11:20:21AM -0800, Lindsay John Lawrence wrote: > Nm... After tinkering in the debugger.. using 'pp etc.. > I get it. I am still getting the (= code data) in picolisp. Cool! Our mails overlapped! :) ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Unexpected behavior
Nm... After tinkering in the debugger.. using 'pp etc.. I get it. I am still getting the (= code data) in picolisp. This gives the results I wanted. (de Fibonacci (N) (let (Fib '(1 1 0) R 0) (do N (rot Fib) (set Fib (+ (cadr Fib) (caddr Fib))) ) (setq R (car Fib)) (set Fib 1 (cdr Fib) 1 (cddr Fib) 0) R ) ) Now I can write... : (Fibonacci 1) -> 2 : (Fibonacci 10) -> 144 : (bench (nil (Fibonacci 10))) 3.388 sec : (length (chop (Fibonacci 10))) -> 20899 digits! Picolisp bignums are awesome! /Lindsay On Sat, Mar 11, 2017 at 10:49 AM, Lindsay John Lawrence < lawrence.lindsayj...@gmail.com> wrote: > Hi > > I am having difficulty understanding the following.. > > This works as expected... > > : (setq Sum '(1 1 0)) > -> (1 1 0) > : (set Sum (+ (cadr Sum) (caddr Sum))) > -> 1 > : (rot Sum) > -> (0 1 1) > : (set Sum (+ (cadr Sum) (caddr Sum))) > -> 2 > : (rot Sum) > -> (1 2 1) > : (set Sum (+ (cadr Sum) (caddr Sum))) > -> 3 > : (rot Sum) > -> (1 3 2) > : (set Sum (+ (cadr Sum) (caddr Sum))) > -> 5 > : (rot Sum) > -> (2 5 3) > : (set Sum (+ (cadr Sum) (caddr Sum))) > -> 8 > : Sum > -> (8 5 3) > > However, when I put it in a function... > > (de Fibonacci (N) >(let (Fib '(1 1 0)) > (do N > (rot Fib) > (set Fib (+ (cadr Fib) (caddr Fib))) ) >(car Fib) ) ) > > The results are something like a 'co routine or 'job... in that Fib as a > memory between calls and it is not re-initialized with the 'let as I > expected. > > -> Fibonacci > : (Fibonacci 1) > -> 2 > : (Fibonacci 1) > -> 3 > : (Fibonacci 1) > -> 5 > : (Fibonacci 1) > -> 8 > ... > > The behavior I expected was > > : (Fibonacci 3) > -> 5 > .. > > Why? > > /Lindsay > > > > >