Labs

Assignments

Deadline Assignment Starting framework Hand in
Fri 14 Sep 0. Introduction None DOMjudge
Fri 21 Sep 1. Lists Assignment1.hs DOMjudge
Fri 28 Sep 2. Data structures Assignment2.hs DOMjudge
Fri 12 Oct 3. Type classes Assignment3.hs DOMjudge
Fri 19 Oct 4. Game, design document Example game Blackboard
Sun 11 Nov 5. Game, implementation Example game Blackboard

Practicals from previous years

Even if you passed the practicals last year, you still need to submit the solutions via DOMJudge and do a new game practical from scratch. The new restrictions on style apply, so you might need to modify your submissions from previous years.

Coding style

The most important question you should ask yourself to judge your own coding style is to ask yourself the question “If another proficient Haskell programmer reads my code, would this be the most readable code I could have written?” Where readable can be understood as the time it would take him or her to understand what your code tries to accomplish and how it accomplishes this.

To give a concrete example: can you figure out what the following function does?

f [a] = h a  -- Base case: call the helper function h on the element in a singleton list
    where h [] = 0  -- Return zero for the empty list
          h (ss:s) = 1 + h s      -- Add one to the recursive call of h.
f (b:a) = if g b > f a then g b else f a      -- If g b is greater than g a then we return g b, otherwise f a
    where g f = foldr (\f g -> g + 1) 0 f   -- Use foldr and a lambda to recurse over f

And what about this one?

-- Find the length of the longest list in a non-empty list of lists.
lengthOfLongestList :: [[a]] -> Int
lengthOfLongestList = maximum . map length

They are equivalent, but it’s much easier to figure out what the second one does due to proper coding style. The first definition would get you zero points for style, the second definition all of them.

Use and reuse

The most important factor that affects readability is proper use and reuse of existing library functions and syntactic sugar. Some important cases:

Indentation

Naming conventions

Comments

Tools

There are two important tools than can give you feedback on your coding style. The first is HLint, a tool designed by Neil Mitchell specifically for giving feedback on your coding style. The second is ghc, the Haskell compiler itself. When you submit your assignment to DOMjudge both tools will be run on your program. You can view the results of these tools by clicking on your assignment in the list of submission you made on in DOMjudge.

You can install HLint by opening a command prompt and typing:

cabal update && cabal install hlint

After HLint has been installed you can run it on your source code (which we’ll assume you have named Assignment.hs) by typing:

hlint Assignment.hs

HLint will then output a list of suggestions for improving your coding style. Most, although not all, of these suggestions will help to improve your coding style and thus your grade. Of course, computer programs sometimes mistake about subjective issues like coding style, so always use your common sense as well. For example, hlint will always suggest you change map f (map g) xs into map (f . g) xs (this is called map fusion). If f and g are simple expressions this will generally be an improvement, but if they are already quite complex, this may not help to improve readability.