Follow the steps below to get started with Haskell programming for the assignments. At the end of this assignment you upload proof of a working Haskell development environment to Canvas in the form of a screenshot.
During the assignments, we expect you to work like a software developer and document and test your code. For each assignment, like this one, we have created a Git repository with a Cabal project. Like similar build tools for other programming languages, you use Cabal to generate your documentation, test your code, build your project, and run your application.
-
Make sure you have installed Git.
-
You should have installed a Haskell development environment that includes
- the Glasgow Haskell compiler (GHC),
- the Cabal build and packaging tool,
- the Haskell language server, and
- the Visual Studio Code editor (VSCode)
when you followed the tutorial 'Set Up Your Haskell Environment'.
-
If you did not do so before, clone this Gitlab repository. Open a command prompt and run the following commands:
cd your-2IPH0-directory git clone git@gitlab.tue.nl:study-material-2iph0/gettingstarted.git cd gettingstarted
If the
git@…URL does not work, try the https varianthttps://gitlab.tue.nl/study-material-2iph0/gettingstarted.gitinstead.
Note: Do not clone your repositories into a cloud-managed folder (e.g. OneDrive), a folder structure that contains spaces (' ') or is too long (>128 characters). This will cause problems with the Haskell build tool, especially on Windows machines. I.e. do not do something like 'C:/OneDrive/Documents/Courses/Bachelor/Computer Science/Year 2/Additional/Long/Folder/Structure/That/Does/Not/Make/Sense/gettingstarted'.
-
Start VSCode. If this is the first time you start VSCode, you will see the Welcome screen:
Otherwise, you will likely see the files and directories you have opened last.
-
Open the directory
gettingstartedvia menu File → Open Folder…. This directory is a Cabal project. If you expand the subdirectoriessrcandtest, your VSCode window looks like: -
Open a terminal in VSCode via menu Terminal → New Terminal. Notice the keyboard shortcut for the next time you want to switch to the terminal. On a wide screen, putting the terminal (1) to the right side of VSCode might be more comfortable. Right-click on the Terminal tab, and select the option Move Panel Right.
Your VSCode window looks like:
-
Run the test suite for this Cabal project by entering the following command in the terminal:
cabal testIf this is the first time running Cabal in this directory, Cabal has to install some packages.
The test fails (2).
If you look in the file explorer (1), you see that Cabal has created the directory
dist-newstyle. You can ignore this directory.Note On Windows you might get some obscure error like:
... realgcc.exe: error: C:\long\path\gettingstarted\dist-newstyle\build\x86_64-windows\ghc-8.10.7\gettingstarted-0.0.0.1\t\gettingstarted-test\build\gettingstarted-test\gettingstarted-test-tmp\Main.o: No such file or directory `gcc.exe' failed in phase `Linker'. (Exit code: 1) ...The likely cause of this error is too long a path to this repository. To resolve this, move the repository to
C:\and try again. -
Fix the function
sayHelloToin the moduleHelloin the filesrc/Hello.hs. Double-click onHello.hsin the file explorer (1). VSCode opens the file in the editor pane (2).While fixing this module, you might want to explore what the function
sayHelloTois doing. You can load and run functions from modules in the REPL (3). Switch to the terminal (use the keyboard shortcut) and run the command:cabal replYou will see the following output in the terminal:
You see that Cabal has loaded the library
gettingstarted-0.1.0.0in GHCi. This library is defined in the filegettingstarted.cabaland contains only thesrc/Hello.hsmodule. In other words, thesrc/Hello.hsmodule is loaded automatically in GHCi.When you enter
sayHelloTo "the teacher"at this GHCi prompt, it responds with:"the teacher"This means that at the moment
sayHelloToechos the argument you supply it. However, it should have responded withHello, the teacher!. -
Change the function
sayHelloToinsrc/Hello.hsto fix this issue. If you want to try your changes, save the file, switch to the terminal, and enter:reload(or:r) at the GHCi prompt. This will reload the module and you can try the updated version in the REPL. -
When you think you fixed
sayHelloTo, save the filesrc/Hello.hs, switch to the terminal, and quit GHCi by entering:quit(or:q). Now rerun the test suite. -
If the test succeeds, make a screenshot of VSCode that shows your solution (3), name, and student ID in the editor (2) and the passing test in the terminal (1). Upload this screenshot to Assignment -1 on Canvas.
For example, for me, this looks like:
Of course, I've blurred the actual solution I came up with!





