In this step we’ll get a Haskell environment up and running.
Preparation
We can run Haskell programs in the browser without installing anything:
To run Haskell on our computer, we have to install the GHC 1 .
If you are on Linux, macOS, or Windows with WSL, then the recommended method of getting a development environment going is Nix with flakes:
- Install Nix
- Enable flakes
-
Run
nix shell nixpkgs#ghc
To proceed without Nix, follow the instructions here.
Verify that you have the GHC in your environment:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 9.0.2
GHCi: the Haskell playground
In either Replit or local installation of the GHC, we can enter an interactive environment with GHCi:
$ ghci
GHCi, version 9.0.2: https://www.haskell.org/ghc/ :? for help
ghci>
If using Nix, here is a quick one-liner:
$ nix shell nixpkgs#ghc --command ghci
Running our first program
Create the file hello.hs
with the following contents:
main :: IO ()
main = putStrLn "Hello, Haskell!"
We can run the program directly with the following command:
$ runhaskell hello.hs
Hello, Haskell!
We can also compile the program to an executable, named the same as the source file:
$ ghc hello.hs
[1 of 1] Compiling Main ( hello.hs, hello.o )
Linking hello ...
$ ./hello
Hello, Haskell!
On Linux and macOS, the executable will have no extension. On Windows it will have the extension .exe
.
-
1
Short for Glasgow Haskell Compiler. GHC Homepage