Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mercury (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == [[Hello world program|Hello World]]: <syntaxhighlight lang="prolog"> :- module hello. :- interface. :- import_module io. :- pred main(io::di, io::uo) is det. :- implementation. main(!IO) :- io.write_string("Hello, World!\n", !IO). </syntaxhighlight> Calculating the 10th [[Fibonacci number]] (in the most obvious way):<ref name="tutorial">Adapted from [http://www.mercurylang.org/documentation/papers/book.pdf Ralph Becket's Mercury tutorial]</ref> <syntaxhighlight lang="prolog"> :- module fib. :- interface. :- import_module io. :- pred main(io::di, io::uo) is det. :- implementation. :- import_module int. :- func fib(int) = int. fib(N) = (if N =< 2 then 1 else fib(N - 1) + fib(N - 2)). main(!IO) :- io.write_string("fib(10) = ", !IO), io.write_int(fib(10), !IO), io.nl(!IO). % Could instead use io.format("fib(10) = %d\n", [i(fib(10))], !IO). </syntaxhighlight> <code>!IO</code> is a "state variable", which is [[syntactic sugar]] for a pair of variables which are assigned concrete names at compilation; for example, the above is desugared to something like: <syntaxhighlight lang="prolog"> main(IO0, IO) :- io.write_string("fib(10) = ", IO0, IO1), io.write_int(fib(10), IO1, IO2), io.nl(IO2, IO). </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)