gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gforth] naive fibonacci


From: Terrence Brannon
Subject: [gforth] naive fibonacci
Date: Sat, 6 Mar 2010 21:04:44 -0500

I [found](http://bit.ly/ar7uDi) a couple of naive fibonacci programs
by Bill Spight that work:

: FIB ?DUP
  CASE
    0 of 1 endof
    1 of 1- RECURSE 1 endof
    1- RECURSE TUCK +
  ENDCASE ;

: FIB DUP 0= IF 1 ELSE 1- RECURSE TUCK + THEN ;

But I would like to know why mine does not work:


: fib { n -- fibn }
  assert( n 0>= )
  n CASE
    0 OF 0 ENDOF
    1 OF 1 ENDOF
    2 OF 1 ENDOF        
    ( otherwise ) n 1 - recurse n 2 - recurse +
  ENDCASE ;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]