uxn/projects/examples/exercises/fib.tal

48 lines
856 B
Tal

( Fibonacci: Numbers made of the two numbers before it )
|0100
@on-reset
#0019 #0001
&l ( -- )
( index ) DUP2 pdec #2018 DEO
( recursive ) DUP2 fib pdec #2018 DEO
( tail-recursive ) DUP2 #0000 #0001 ROT2 fibr pdec
( lb ) #0a18 DEO
POP2 POP2 INC2 GTH2k ?&l
POP2 POP2
( end ) #800f DEO
BRK
(
@|recursive )
@fib ( num* -- numfib* )
#0001 GTH2k ?&>ok
POP2 JMP2r &>ok
( a ) SUB2k fib STH2
( b ) INC2 SUB2 fib STH2r
( res ) ADD2 JMP2r
(
@|tail-recursive )
@fibr ( a* b* num* -- a* b* numfib* )
DUP2 #0001 NEQ2 ?&>no-1
POP2 DUP2 JMP2r &>no-1
( a ) #0001 SUB2 STH2
( b ) SWP2 ADD2k NIP2 STH2r !fibr
(
@|stdlib )
@pdec ( short* -- )
#2710 LIT2r 00fb
&w ( -- )
DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?&>skip
DUP LIT "0 ADD #18 DEO
INCr &>skip
POP2 #000a DIV2 SWPr INCr STHkr ?&w
POP2r POP2 POP2 JMP2r