Hello World (Hume)

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 | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | Fortress | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | 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 | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This example shows the well-known "Hello World!" example implemented in Hume as output of a counter automaton.

Lets specify output

<<I/O bindings>>=

-- I/O bindings

stream output to "std_out" ;

Define automaton (box in Hume) inputs, outputs and body. This is the main program loop. Each box runs in a separate thread.

<<box definitions>>=

-- box definition

box hello
in (count_in::int 32)                                    -- inputs
out (count_out::int 32, shown::(string, int 32, char))   -- outputs (result roles and types)

-- body

match    -- match patterns structure must conform to inputs
         -- corresponding expressions must conform to the declared 'out' type
         -- declaration role names are in sole interest of wiring statements

 99 -> (0, ("Hello World ", 99,'\n'))    -- reset count_out to 0
 | count -> (count +1, ("Hello World ", count,'\n')) ;   

Wire box (thread) outputs and inputs generating mailboxes

<<wirings>>=
wire hello 
   -- inputs wiring for "in" parameters
   (hello.count_out initially 0) -- get hello.count_in from hello.count_out 

   -- outputs wiring for "out" result tuple
   (hello.count_in, output) ;  -- put hello.count_out to hello.count_in, hello.shown to output

The whole program

<<HelloWorld.hume>>=

-- HelloWorld.hume

  I/O bindings

  box definitions

  wirings

Execute with the Hume interpreter

./hume HelloWorld.hume

and the output is

Hello World 0
Hello World 1
Hello World 2
...

Alternatively the construct expression is equivalent to a box with no inputs, and one output to std_out

The program does not end until you type Ctrl+C.

<<HelloWorld2.hume>>=

-- HelloWorld2.hume

expression "Hello World\n" ;

Download code
Personal tools