Skip to main content
Department of Information Technology

Haskell Exercises

Sorting

Implement merge sort in Haskell. Check that your sort works for different data types.

Lazy Evaluation

Compute the (infinite) list of all integers that can be expressed as products of 2, 3, and 5. First consider the problem of computing the list of products of 2 and 3. Use take to look at the first elements of the list. The first ten elements should be [2,3,4,5,6,8,9,10,12,15].

Type Classes

Define a type class Ugly which defines a function
ugly :: Ugly a => a -> Bool
which recognizes ugly values. Make some instances of this class. For example, odd integers night be ugly but even numbers not. Lists are ugly if most elements in them are ugly. Define a polymorphic function that takes a list and returns a list of the elements that are not ugly.

Random Laziness

Write a function that generates an infinite list of pseudo-random numbers in the interval 0..(n-1)

random :: Int -> Int -> [Int]
The first argument should be a seed, and the second should give the size of the interval (n). For example, random 123 2 might generate

[1,0,1,1,0,0,0,0,1,1,0,1,1,1,0,0,1,1,...]

Minor Language

  • Define a type for representing mathematical expression, containing at least ordinary arithmetic.
  • Use type classes Show and Read to convert your representation to and from strings.
  • Write a evaluator for expressions.
  • Include functions, i.e., lambda abstractions!

Updated  2012-12-03 20:49:31 by Cons T. Åhs.