Want some programming help? [close]

Hello World in Haskell

Haskell is a purely functional programming language create in 1990. This Hello World example will help you get started with Haskell. Install Haskell for your system from the Haskell website. The program installed is called ghc which stands for the Glasgow Haskell Compiler. It can be run interactively or as a compiler.

Running Interactively

To run interactively invoke Haskell with

ghc --interactive

Once in the REPL, you can start entering commands. Try this:

putStrLn "Hello, world!"

There's your hello world! The function putStrLn outputs a string as a line. The interactive mode accepts commands that are prefixed with a colon. Here are a couple commands you can try out.

:set prompt ">>>"
:show languages
:cd C:\Users\NanoDano\haskell
:show modules
:load hello.hs
:show modules
:main
:quit

Compiling Haskell

Instead of running interactively you can compile files to create an executable. The -o option lets you specify the name of the output file. The input file in this example is hello.hs

We can't just drop in a putStrLn call the way we would with something like Python. We have to define a main function. When the program is run that will be the main entry point, just like all other languages. Here is the contents of hello.hs.

main = putStrLn "Hello, world!"

Compile it with

ghc -o hello hello.hs

And run it with

./hello

Advertisement