2. Introduction to Haskell

In mathematics, functions have a domain an codomain. In Haskell, functions have types or signatures. They often can be omitted in Haskell, but can also be explicitly written as in:

f :: Integer -> Integer -> Integer
f x y = x + y

or:

f :: Bool -> Bool
f True = False
f False = True

The previous example illustrates that we can define functions by specifying a behaviour for given values.

2.1.1. Write a function together with its signature, which implements boolean AND:

myand :: Bool -> Bool -> Bool
...

2.2.1. Write an implementation for a function ifp which takes a boolean, expressions $ e_1$ and $ e_2$ and returns $ e_1$ if the boolean is true and $ e_2$ otherwise.

ifp = ...

2.2.2. Write a function which takes three integers and returns the largest. Hint - sometimes parentheses are important in function calls.

f :: Integer -> Integer -> Integer -> Integer

In Haskell, we can use the if construct, in a manner almost identical to the above implementation, e.g.

f x = if x == 0 then 1 else 0

The previous function returns 1 if x is equal to 0 and 0 otherwise. A more elegant way is to use guards:

f x
   | x == 0 = 1
   | otherwise = 0

More generally, guards can be used as follows:

<function_name> <parameters>
  | <boolean_condition_1> = <expression_1>
  | <boolean_condition_2> = <expression_2>
  ...
  | otherwise = <expression_n>

2.2.3. Solve the previous exercise using guards.

The following code examples illustrate the usage of lists:

-- defining a new list
l = [1,2,3]
 
-- adds x to the beginning of a list l. The operator : is called 'cons' and it is infix (just like +)
cons x l = x:l
 
-- the previous list can be defined also as:
lp = 1:2:3:[]
 
-- function head returns the first element of a list 
first_of_l = head l
 
-- function tail removes the first element of a list and returns the result
remove_first_of_l = tail l
 
-- the infix operator ++ concatenates lists.
lpp = l ++ lp

2.3.1. Implement reversal

2.3.2. Write a function which extracts the third to last integer from a list and returns True, if that number is odd (hint: the function mod may be useful), and false otherwise. If the list has fewer than three elements, the function should also return false.

            V
   f [3,4,5,2,3,9] = False
   f [3,4,2,1,4,4] = True

Using head and tail can become tedious if we need to extract specific inner elements of a list. Alternatively, we can use list patterns in function definition, in order to explore the structure of a list. A few examples are given below.

-- this function uses patterns to test if a list is empty. The pattern 'x:xs' refers to a list where the first element is x and the rest of the list is xs
is_empty [] = True
is_empty (x:xs) = False

2.3.3. Implement the previous exercise using patterns

2.3.4. Implement a function which returns the sum of integers from a list.

2.3.5. Implement a function which takes a list of booleans and returns false if at least one boolean from the list is false.

2.3.6. Implement a function which filters out all odd numbers from a list.

2.3.7. Implement a function which takes a list of booleans and returns a list of integers. In the latter, (True becomes 1 and False becomes 0). Example: f [False, True, False] = [0,1,0].

We can construct more complicated patterns from simpler ones:

-- this function returns true if the list given as parameter has at least tree elements.
f (x:y:z:xs) = True
f _ = False

The following example illustrates the usage of Char and String. In Haskell, String is a type-alias for [Char] (strings are lists of chars and can be introspected using pattern matching as well).

-- this function returns true if the parameter is the character 'a'
f 'a' = True
f _ = False
 
-- this function returns true if the given string has the SECOND character equal to 'b' and false otherwise (or if the string has less than two characters)
g (_:'b':xs) = True
g _ = False

2.4.1. Write a function which removes all empty strings from a list.

2.4.2. Write a function which removes all strings of size smaller than 3. Do not use the builtin function length.

2.4.3. Write a function which removes all strings having the third letter equal to 'a'.

2.4.4. Write a function which:

  • removes all strings which are not names. A name always starts with an uppercase.
  • removes the last name from the resulting list of names. A name always comprises of the first name followed by the last name.