Hello World (Mercury)
From LiteratePrograms
- 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
This example shows the well-known "Hello World!" example implemented in Mercury.
<<hello_world.m>>= :- module hello_world. :- interface. :- import_module io. :- pred main(io.state, io.state). :- mode main(di, uo) is det. :- implementation. main(!IO) :- io.write_string("Hello, World!\n", !IO).
The "!X" construct encloses two variables, the initial "!.X" and final "!:X" states of a state variable.
This does not demonstrate much of the Mercury language. A short tutorial that provides further information on Mercury can be found here.
Testing
After installing the Mercury distribution, we test the hello_world program from the system console
- compile hello_world.m
- then test the hello_world executable
mmc hello_world.m hello_world
will produce the output
Hello, World!
With DCG Notation.
DCG notation is intended for writing parsers and sequence generators in a particular style; in the past it has also been used to thread an implicit state variable, typically the IO state, through code.
DCG-rules in Mercury have identical syntax and semantics to DCG-rules in Prolog.
Here DCG notation saves us to specify the !IO states construct, which is added as parameter to all calls in the routine.
This is not convenient when not all statements should carry the state parameters.
<<hello_world2.m>>= :- module hello_world. :- interface. :- import_module io. :- pred main(io.state, io.state). :- mode main(di, uo) is det. :- implementation. main --> io.write_string("Hello, World!\n").
Download code |