Hello World in Haskell

Our first program will print the classic “hello world” message. Here’s the full source code.

module Main where

main :: IO ()
main = putStrLn "hello world"

To run the program, save the code in a file named HelloWorld.hs and use runhaskell to execute it.

$ runhaskell HelloWorld.hs
hello world

Sometimes we’ll want to compile our programs into binaries. We can do this using ghc.

First, compile the code:

$ ghc -o HelloWorld HelloWorld.hs
$ ls
HelloWorld  HelloWorld.hi  HelloWorld.hs  HelloWorld.o

We can then execute the built binary directly.

$ ./HelloWorld
hello world

Now that we can run and build basic Haskell programs, let’s learn more about the language.