Skip to content

Commit f268a4d

Browse files
committed
Add Fibonacci in Forth
1 parent 63e770e commit f268a4d

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

archive/f/forth/fibonacci.fth

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
: fibonacci ( n -- )
2+
0 1 rot 0 ?do \ prepare the stack and loop
3+
tuck + \ calculate the next fibonacci number
4+
i 1+ 0 .r ." : " \ print the current index
5+
over 0 .r cr \ print the current fibonacci number
6+
loop
7+
2drop ;
8+
9+
: usage ( -- ) ." Usage: please input the count of fibonacci numbers to output" cr ;
10+
11+
: main
12+
argc @ 2 < if usage exit then
13+
1 arg s>number? 0= if usage exit else drop then
14+
fibonacci ;
15+
16+
main
17+
bye

0 commit comments

Comments
 (0)