Navigation
Synopsis Variations on the ubiquitous Hello World example.
Examples

First version: hello on the command line

We demonstrate hello via an interactive session with the Rascal system. First we get the prompt rascal> that shows that Rascal is ready for our input. Next, we import the library module Rascal:IO since hello world requires printing. Rascal responds with the feedback ok so we know that all went well. Finally, we call println and proudly observe our first Rascal output!
rascal>import IO;
ok
rascal>println("Hello world, this is my first Rascal program");
Hello world, this is my first Rascal program
ok

Second version: hello in a function

A slightly more audacious approach is to wrap the print statement in a function and call it:
rascal>import IO;
ok
rascal>void hello() {
>>>>>>>   println("Hello world, this is my first Rascal program");
>>>>>>>}
void (): void hello();
When you type in a command and continue it on a new line the Rascal systems prompts you with >>>>>>> to indicate that more input is needed. Don't get scared by the void (): void hello(); that you get back when typing in the hello function. The first void () part says the result is a function that returns nothing, and the second part void hello() summarizes its value (or would you prefer a hex dump?). Finally, we call the hello function and enjoy its output.
rascal>hello();
Hello world, this is my first Rascal program
ok

Third version: hello in a module

The summit of hello-engineering can be reached by placing all the above in a separate module:
module demo::basic::Hello

import IO;

void hello() {
   println("Hello world, this is my first Rascal program");
}
Using this Hello module is now simple:
rascal>import demo::basic::Hello;
ok
rascal>hello();
Hello world, this is my first Rascal program
ok
The hello function is by default visible outside the Hello module. We could stress this by adding writing public void hello() { ... }. Restricting visibility to the module itself can be achieved by adding the keyword private to the definition of hello.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.