Say hello to my little friend
In this section, you will write the traditional “Hello, world” program. This section assumes you have an environment setup on your computer where you can write, compile, and execute Haskell code. Refer to the section Glasgow Haskell Compiler for general guidance.
GHCi
Open a terminal or command window in your operating system. Type the command ghci
to load the interactive version of GHC, called GHCi. GHCi is a REPL (Read-Eval-Print-Loop) environment for Haskell. The character i
means interactive. Your terminal would show something like below.
1
2
3
$ ghci
GHCi, version 9.0.2: https://www.haskell.org/ghc/ :? for help
ghci>
The first line shows the executable that loads GHCi. Do not worry about the second line. It merely shows various basic information about GHC. The third line, i.e. ghci>
, is the prompt. You are meant to type Haskell code, or GHCi commands, following the character >
. Some versions of GHCi would show their prompts as Prelude>
when you run ghci
.
‘Allo ‘Allo!
The “Hello, world” program is the traditional1 first program you would write when learning a programming language. Let’s succumb to peer pressure and write your first program in Haskell.
Enough with the boring introduction. How would you get Haskell to print the string "Hello, world"
? Use the function print
. Here is a sample terminal session:
1
2
ghci> print "Hello, world"
"Hello, world"
So far so good.
Some people dislike quotation marks in a printed string. Use the function putStr
to print a string to standard output, without including the quotation marks.
1
2
ghci> putStr "Hello, world"
Hello, worldghci>
The function putStr
does indeed print a string and exclude quotation marks. However, as the above terminal session shows, putStr
does not include a newline character after printing the string, like the function print
does. What we need is the best of both worlds. The function putStrLn
can:
- Print a string to standard output.
- Exclude quotation marks.
- Include a newline character at the end of the printed string.
Here is a sample terminal session:
1
2
ghci> putStrLn "Hello, world"
Hello, world
Tales from the script
The Haskell REPL, i.e. GHCi, is suitable for quick experimentation. Sooner or later you would want to store Haskell code in a text file. Create a text file called hello.hs
and save the following code in the file:
1
main = print "Hello, world"
Ignore the code segment main =
. We will discuss that in the section The function main
.
Now use GHC to compile your source file to machine code. The compiler is called ghc
. The following terminal session demonstrates the compilation and execution process.
1
2
3
4
5
6
7
8
9
$ cat hello.hs
main = print "Hello, world"
$ ghc hello.hs
[1 of 1] Compiling Main ( hello.hs, hello.o )
Linking hello ...
$ ls
hello hello.hi hello.hs hello.o
$ ./hello
"Hello, world"
For our purposes, the important files shown in the above terminal session are:
hello.hs
– The source file.hello
– The executable file, in machine code.
The other files generated by the compiler should not be of concern at the moment.
You can also load your source file from within GHCi. At the REPL prompt, use the command :load
to load your source file. The command can be shortened as :l
with a lowercase version of L. The above terminal session can be achieved within GHCi as follows:
1
2
3
4
5
ghci> :load hello.hs
[1 of 1] Compiling Main ( hello.hs, interpreted )
Ok, one module loaded.
ghci> main
"Hello, world"
Notice that GHCi does not compile the provided source file. In fact, GHCi interprets the given source file. This feature of GHCi is similar to programming languages that offer a REPL environment.2
The function main
Recall the “Hello, world” program from the section Tales from the script, reproduced below:
1
main = print "Hello, world"
The program creates a function called main
. The body of the function is the line:
print "Hello, world"
which is assigned as the definition of main
via the assignment operator =
. The function main
has a special meaning in Haskell.3 The function is the entry point of your program. Execution starts from the function main
. The function, if it exists, is automatically called whenever you run the compiled binary file.
Exercises
Exercise 1. Load GHCi and print your name to standard output. Repeat the exercise, but using the official Haskell playground.
Exercise 2. Edit the file hello.hs
to use the function putStr
to print your name to standard output. Repeat the exercise, but using the function putStrLn
.
Exercise 3. Instead of using double quotation marks (i.e. "Hello, world"
), use single quotation marks (i.e. 'Hello, world'
) to print the string "Hello, world"
to standard output. What does GHCi or the compiler say? What do you think might be the cause and why?
Exercise 4. GHCi has various commands to help you while you experiment within the REPL environment. Here is an exhaustive list of all GHCi commands. Browse through the documentation of each command. Do not be overwhelmed by the sheer volume of documentation. For now, you might want to read up on the commands :help
, :load
, :quit
, and :reload
.
Exercise 5. Read more about GHCi here. Do not be overwhelmed by the massive volume of text. Browse through the first two or three sections to see what GHCi has to offer.
Exercise 6. You know that GHCi can load and interpret a source file, executing the source code without having to compile it first. Furthermore, GHC can compile your source code to a binary file that contains the native machine code of your computer. The command line utility runghc
(also named runhaskell
) offers a third way to run Haskell code. The command runghc
can run your Haskell source file without compiling it first. Use runghc
to run the file hello.hs
.
Exercise 7. You can try out Haskell in your browser by running an interactive tutorial.