- 8 minutes
In this first lesson we demonstrate working with the basic Haskell tools so you can follow along with everything we’ll be doing in this course.
We’ll be repeating the same demonstration three times:
The first two will assume that you have Haskell already installed on your computer. If you don’t already have that set up, then for this course you should be using the third option – Repl.it just runs in a web browser, and so you can use that without any setup.
If you do have Haskell installed, start by creating a directory for this course.
Text editor and terminal
For this setup, open two windows side-by-side:In the video we open the two side-by-side in one terminal using tmux; opening the two applications using a graphical window manager is also fine.
- On the left, open a text editor.In the video we use vim, though any editor will do.
- On the right, run
‘GHC’ is the name of the Haskell compiler, and ‘i’ stands for ‘interactive’.This course will cover everything you need to know about GHCi to start. We also have another course – The interactive compiler – that goes into GHCi in much greater detail. So GHCi is the interactive interface to the Haskell compiler.
Using the GHCi prompt
In GHCi we can do things like…
Define variables – we’ll define
λ> x = 3
Evaluate expressionsGHCi is sometimes referred to as a “REPL”, or read–eval–print loop, which describes its ability to evaluate expressions typed at the prompt – although it can do much more than this. – if we ask for the value of
x + 2, it tells us that it’s
λ> x + 2 5
Run actions – let’s say we want to print the string “hello” to the terminal. The
putStrLnfunction does that. (That stands for “put string line”.)
λ> putStrLn "hello" hello
Notice that we’ve used two functions so far: the plus sign (
+) and the
putStrLn function. We didn’t have to import either of these. That’s because these things come from the standard library, a moduleLater in this course we’ll talk a bit more about what modules are and what it means to import them. called
Prelude, which is imported by default. If, hypothetically, it weren’t, we could have written
λ> import Prelude
We can write that, but we don’t have to, because this import is already there implicitly.
Saving definitions in a file
What we can’t do in ghci is save the definitions we write. The prompt also not very good for entering large expressions, especially ones that span multiple lines. For that, we’re going to want the text editor.
Create a file called
.hs is always the file extension for Haskell source files. In this file, we can write definitions. For example:
100 this time, added a new variable
y defined to be a number that is two greater than
x, and our
putStrLn "hello" action here is assigned the name “main”.
So we can write definitions in the text editor, but what we can’t do is evaluate the expressions or run the action. For that, we have to go back over to GHCi. Once we save that file, we load it into the REPL. Use the
:load command and specify the name of the file we want to load, which is
λ> :load main.hs [1 of 1] Compiling Main ( main.hs, interpreted )Ok, one module loaded.
GHCi tells us that it’s now loaded. Now in the REPL we can, for example, look at the values of
λ> x 100 λ> y 102
We can run that
main action, and it prints “hello”.
λ> main hello
Changing and reloading
Make some changes to the
main.hs file. Let’s say we now want
x to be
200 instead of
100, and have our greeting be more emphatic.
Save the file, and reload it into GHCi using the
λ> :reload [1 of 1] Compiling Main ( main.hs, interpreted )Ok, one module loaded.
You can also abbreviate that as
:r, which does the same thing.
λ> :r Ok, one module loaded.
Now if we look at
y again, we see their new values
λ> x 200 λ> y 202
main is now excited.
λ> main hello!!!!!
Sometimes we make mistakes. Suppose we misspelled the name of
putStrLn. Modify the
main.hs file as follows:
:reload once more. When GHCi tries to load the code with the misspelling, it shows us an error message.
λ> :reload [1 of 1] Compiling Main ( main.hs, interpreted ) main.hs:5:8 error: • Variable not in scope: putStrLtr :: [Char] -> t • Perhaps you meant ‘putStrLn’ (imported from Prelude) | 5 | main = putStrLtr "hello!!!!!" | ^^^^^^^^^Failed, no modules loaded.
Please take some time to read the error message carefully, because it contains a lot of information.
main.hs:5– The error occurred in the file
main.hson line 5.
Variable not in scope: putStrLr– There is no such function as
Perhaps you meant ‘putStrLn’– GHCi guesses correctly that what we really wanted was the function named
The error output also includes an excerpt from the file, with an underline highlighting where we made the mistake. The line numbering to the left of the excerpt also reminds us again that this is line 5.
| 5 | main = putStrLtr "hello!!!!!" | ^^^^^^^^^
putStrLtr back to
putStrLn, reload the code, and run
main in GHCi to verify that it works correctly again.
If you prefer to use Visual Studio Code, the setup is still very similar. Visual Studio is convenient because it has a side panel with a built-in terminal.
Create a file called
Open the terminal in the panel on the right. Run
ghci in that terminal and
:load the source file. For the demonstration as before: query the values of
y, and run the
Repl.it runs in a web browser and is your quick-start option if you don’t have Haskell installed on your computer yet. It does have some limitations, but it will server just fine for the purposes of this course.
Go to the https://repl.it/ website, click the “start coding” button, and choose Haskell from the list of language choices. Then click “create repl”.
Repl.it automatically starts us off with a file called
main.hs, and it has already opened GHCi on the right.
- Again, in our source file we can type some definitions.
- We still have to use the
:loadcommand to load that
main.hsfile into GHCi.
- Again, we can query the defined values and run the
Repl.it also has a “run” button at the top. When we click on that, it reloads and runs
main. If you are working on a program and often testing its
main action, you may find this button convenient.