Haskell Get Started
Getting started means saving a Haskell program in a file and running it with the GHC compiler. GHC is the tool that turns Haskell code into a program the computer can run.
A saved program prints its message
Save this code in a file named Main.hs. Compile it and run it to see the message.
main :: IO ()
main = putStrLn "Ready to run"main is where running begins
The built program looks for main first. It carries out the action written after the equals sign.
main :: IO ()
main = putStrLn "Starting here"A compiled program can print a result
The same file, compile and run steps work for a calculation. The result appears when the program runs.
main :: IO ()
main = print (2 + 3)Example
main :: IO ()
main = putStrLn "My first Haskell program"Key points
- File: Haskell code is saved in a file whose name ends with .hs.
- Compiler: a tool that turns source code into a runnable program.
- GHC: the standard compiler for Haskell programs.
- Compile: running ghc Main.hs builds a runnable program from the file Main.hs.
- Run: running the built program carries out its main action.
- Signature: the line main :: IO () labels main as the program's starting point.
In this example, the code is a complete program ready to save in a .hs file. Compiling it with GHC and running it prints the text after putStrLn.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Haskell Playground
Exercise
Which file name is right for a Haskell source file?