0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-16 11:15:06 +00:00
uxn/projects/examples/exercises/fib.tal

48 lines
818 B
Tal
Raw Normal View History

2023-07-21 17:18:49 +00:00
( 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
2023-06-02 04:53:28 +00:00
POP2 POP2
2023-07-21 17:18:49 +00:00
( end ) #800f DEO
BRK
2022-02-23 23:23:38 +00:00
2023-07-21 17:18:49 +00:00
(
@|recursive )
2023-06-06 18:49:28 +00:00
2023-06-06 21:33:20 +00:00
@fib ( num* -- numfib* )
2023-07-21 17:18:49 +00:00
#0001 GTH2k ?&>ok
POP2 JMP2r &>ok
2023-07-21 17:42:04 +00:00
SUB2k fib STH2
INC2 SUB2 fib STH2r
ADD2 JMP2r
2023-06-02 04:53:28 +00:00
2023-07-21 17:18:49 +00:00
(
@|tail-recursive )
2023-06-06 18:49:28 +00:00
2023-06-06 21:33:20 +00:00
@fibr ( a* b* num* -- a* b* numfib* )
2023-07-21 17:42:04 +00:00
#0001 NEQ2k ?&>no-1
POP2 POP2 DUP2 JMP2r &>no-1
SUB2 STH2
SWP2 OVR2 ADD2 STH2r !fibr
2023-06-06 18:49:28 +00:00
2023-07-21 17:18:49 +00:00
(
@|stdlib )
2023-06-06 18:49:28 +00:00
2023-06-02 04:53:28 +00:00
@pdec ( short* -- )
#2710 LIT2r 00fb
2023-07-21 17:18:49 +00:00
&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
2023-06-02 04:53:28 +00:00