gforth 0.7.0Version of implementation Gforth of programming language Forth
A version of Gforth, released on December 22, 2008.
." reads a double-quotes-delimited string and outputs it. The space character that separates
Hello doesn’t count as part of the string, it’s necessary to recognize
." as a word.
." Hello, World!" cr
This example uses recursive factorial definition. Forth is a stack-oriented language, so all commands (words) perform stack manipulations. Thus,
dup duplicates the topmost element of the stack, a constant pushes itself on the top of the stack,
> compares second-topmost element to the topmost element and pushes the result,
- subtracts topmost element from second-topmost, etc.
: fac recursive dup 1 > IF dup 1 - fac * else drop 1 endif ; : lp swap 1 + swap do i . i ." ! = " i fac . cr loop ; 16 0 lp
This example uses iterative definition of Fibonacci numbers.
: fib-iter 0 1 rot 0 ?do over + swap loop drop ; : lp 1 do i dup fib-iter . ." , " loop drop ; 17 lp ." ..."
This example uses recursive definition of Fibonacci numbers.
: fib-rec dup 2 u< if exit then 1- dup recurse swap 1- recurse + ; : lp 1 do i dup fib-rec . ." , " loop drop ; 17 lp ." ..."