Hello World (Forth)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Alice ML | Amiga E | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | C++ | C# | Dylan | E | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | IBM PC bootstrap | Inform 7 | Java | JavaScript | LaTeX | Logo | Lua | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Perl | PHP | PIR | PLI | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | SQL | Standard ML | Tcl | Tcl Tk | Visual Basic | XSL

A simple Forth program that prints a message to the screen from the interpreter.

The word .( scans ahead in the input buffer for a matching ) and displays the intervening string. The initial space is ignored, since it is required to separate the word from the text to display.

The word CR (for Carriage Return) prints a newline.

<<hello_world.fs>>=
CR .( Hello World!)

To define a word that prints "Hello World!" you would use .", which scans ahead in the input buffer for a matching " and compiles code to display the intervening string. The initial space is again ignored, since it is required to separate the word from the text to display.

<<hello_world.fs>>=
: hello   CR ." Hello World!" ;
Download code
hijacker
hijacker
hijacker
hijacker