Hello World (occam)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

These simple occam programs print a string to the console, then print the integers 1 through 10 using a simple for loop. The first of the two programs demonstrates the use of the classic INMOS hostio library for writing to the screen, and follows the standard occam 2.1 syntax. The second program demonstrates the occam-π syntax implemented in the modern KRoC occam compiler.

Hello world in classic occam

One of the unusual features of occam is that it requires blocks of code to be explicitly identified as sequentially executed (SEQ) or concurrently executed (PAR). This program is purely sequential. The first two lines import some definitions for the hostio library, and instruct the compiler to link the program with the shared hostio library. The third line defines a procedure with two arguments, both channels which use the SP protocol (defined by the hostio library). These channels are necessary to perform I/O. The so functions are defined by hostio, and permit output of various types of data.

(Occam uses the indentation depth to define block structure, a language feature later copied by python. Also Occam was the first language to be tied to an IDE which featured a folding editor.)

<<hello_world-classic.occ>>=
#INCLUDE "hostio.inc"
#USE "hostio.lib"
PROC hello.world (CHAN OF SP fs, ts)
  SEQ
    so.write.string.nl(fs, ts, "Hello World!")
    SEQ i = 1 FOR 10
      SEQ
        so.write.int(fs, ts, i, 0)
        so.write.nl(fs, ts)
:

Hello world in occam-π

The modern KRoC occam compiler deprecates the use of hostio in favor of a more POSIX-like approach to I/O. I/O procedures are imported from the oddly named course.lib shared library (the name is a relic of occam-π's academic roots).

<<hello_world-kroc.occ>>=
#USE "course.lib"
PROC hello.world (CHAN BYTE scr!)
  SEQ
    out.string("Hello World!*n", 0, scr)
    SEQ i = 1 FOR 10
      SEQ
        out.int(i, 0, scr)
        scr ! '*n'
:

Note that the course.lib I/O procedures use channels that follow the BYTE protocol, rather than the SP protocol used by hostio.lib. As a result, individual characters can be written to the screen via direct output through the scr channel. Thus,

scr ! '*n'

sends the newline character to the screen (occam uses the * character as its escape character, rather than the more common \ - this is a convention that dates back to earlier languages such as Algol60 and BCPL).

Running

These programs can be compiled using the Kent Retargetable occam Compiler. On UNIX, you can build and run the samples using the following scripts:

<<build_and_run-classic.sh>>=
#!/bin/bash
kroc --octran-opts="--tlp-fsts" -lhostio -lconvert -o hello_world-classic hello_world-classic.occ
./hello_world-classic
<<build_and_run-kroc.sh>>=
#!/bin/bash
kroc -lcourse -o hello_world-kroc hello_world-kroc.occ
./hello_world-kroc
Download code
Views
Personal tools