Part 15

Intro Haskell

About the Course

This course is adapted from the Haskel Mooc , created by Joel Kaasinen (Nitor) and John Lång (University of Helsinki), and adapted for Concepts of Programming Languages as taught on the Radboud University, Nijmegen.

Note that, even though we are hosting this part of the course on the same website, we will not be using the TMC system anymore. Instead of that, you will run your own code and tests locally, and then submit your code again to BrightSpace. Working on the exercises involves knowing how to use the command line, and basic usage of the Git version control system.

Running Haskell

We will spend this first section setting everything up. Since everything takes a bit of time to install, we recommend following through on this chapter as soon as possible. Do not worry if you don't understand anything yet, that will come in the next page.

The easiest way to get Haskell is to install the stack tool, see https://haskellstack.org. The exercises on this course are intended to work with Stack, so you should use it for now.

By the way, if you’re interested in what Stack is, read more here.

For now, after installing Stack, just run stack ghci to get an interactive Haskell environment. Try running 1+1 to see if it works. You can type :quit to leave.

Working on the Exercises

The original course materials, including exercises, are available in a Git repository on GitHub at https://github.com/moocfi/haskell-mooc. We recommend cloning this repository, you can do that by running the following command in the directory where you want to store the course:

git clone https://github.com/moocfi/haskell-mooc.git

Once you’ve cloned the haskell-mooc repository, go into the exercises directory (by typing cd haskell-mooc/exercises/). To download and build dependencies needed for running the exercise tests (such as the correct version of GHC and various libraries), run following command in your terminal:

stack build

Note that the dependencies are multiple gigabytes and it will take a while for the command to finish. So please do this as soon as possible.

Note! Here are some fixes for common problems with stack build:

  • If you get an error like While building package zlib-0.6.2.3, you need to install the zlib library headers. The right command for Ubuntu is sudo apt install zlib1g-dev.
  • If you get an error like Downloading lts-18.18 build plan ... RedownloadInvalidResponse, your version of stack is too old. Run stack upgrade to get a newer one.

There are primarily two types of files in the exercises directory: exercise sets named SetNX.hs and accompanying test program for the exercises named SetNXTest.hs. Both are Haskell source files, but only the exercise file should to be edited when solving the exercises. Instructions to all individual exercises are embedded in the exercise file as comments.

Just to make sure you set up everything correctly, check out the first exercise set in Set1.hs . Don't worry if you don't understand anything yet. Try running the tests on the exercise, using the following command:

stack runhaskell Set1Test.hs

If you set up everything correctly, your output will look something like this:

===== EXERCISE 1
*** Failed! Falsified (after 1 test):
You haven't defined 'one' yet!

*** Failed! Falsified (after 1 test):
The type of 'one'
  Expected: Int
  Was: nothing

*** Failed! Falsified (after 1 test):
You haven't defined 'two' yet!

*** Failed! Falsified (after 1 test):
The type of 'two'
  Expected: Int
  Was: nothing

----- Fail
===== EXERCISE 2
00000 Todo
===== EXERCISE 3
00000 Todo
===== EXERCISE 4
00000 Todo
===== EXERCISE 5
00000 Todo
===== EXERCISE 6
*** Failed! Falsified (after 1 test):
checkPassword "mellon"
  Expected: "You're in."
  Was: "ACCESS DENIED!"

----- Fail
===== EXERCISE 7
00000 Todo
===== EXERCISE 8
00000 Todo
===== EXERCISE 9
00000 Todo
===== EXERCISE 10
00000 Todo
===== EXERCISE 11
00000 Todo
===== TOTAL
0____0_____
0 / 11

We can see that all tests now fail or give a Todo. When you pass a test, it will look like:

===== EXERCISE 1
+++++ Pass

Our goal in this course is to pass all exercises!

To make debugging faster and more straightforward, I can load my exercise file in GHCi, which allows me to evaluate any function manually. Suppose I want to check the quadruple function from the first exercise set, I can run:

$ stack ghci Set1.hs
GHCi, version 9.2.8: https://www.haskell.org/ghc/  :? for help
[1 of 2] Compiling Mooc.Todo        ( Mooc/Todo.hs, interpreted )
[2 of 2] Compiling Set1             ( Set1.hs, interpreted )
Ok, two modules loaded.
*Set1> quadruple 1
2

For your convenience, as an edit from the original material, we have put the exercises after when we think you should be able to make them. After you have made all exercises and passed all tests in a set, for example Set1.hs, you can upload it in the corresponding assignment on Brightspace.

We run the same test as you have locally, so if you pass them locally, you already know that you have passed the assignment.

The handing in via Brightspace is mostly for administration.

You can make the exercises in any IDE you like. If you want to practice with working with the command line, try making all exercises with Vim or Nano. However, making them in VS Code or any other text editor is also fine, as long as you pass the tests.

Extra material

In addition to this course material, the following sources might be useful if you feel like you’re missing examples or explanations.

We especially recommend going through "Learn You a Haskell for Great Good!", whenever you feel lost.

You have reached the end of this section! Continue to the next section:

You can check your current points from the blue blob in the bottom-right corner of the page.