1. Introduction to the Haskell language
The main (and, as we shall soon see, the only!) programming instrument in Haskell is the function. In mathematics, a function is a relation between specified sets ($ f:A\rightarrow B$ ) that associates to every element of $ A$ , exactly one element of $ B$ . This is precisely the interpretation of a function in Haskell. (Not to be confused with OOP methods or procedures).
The identity function is defined in Haskell as:
f x = x
1. Write a constant function in Haskell.
2. Write the Haskell implementation of the function below: $ f(x,y) = x$ (the Ox projection function)
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
3. Write a function together with its signature, which implements boolean AND:
myand :: Bool -> Bool -> Bool ...
4. What is the signature of the solutions for exercises 1. 2. ? Use :t
to find out. What does a
mean?
5. Write an implementation for:
if' :: Bool -> a -> a -> a
- How many parameters does
if'
take? - What is the interpretation of each parameter?
- What should
if'
return?
6. Write a function which takes three integers and returns the largest. Hint - sometimes parentheses are useful.
f :: Integer -> Integer -> Integer -> Integer
7. What is the type of the function f
defined below:
f x y z = if x then y else z
8. What is the type of:
f x y z | x == True = y | otherwise = z
In the above implementation, the body (or expression) of the function f
is defined similarly to a branch-definition in mathematics. In Haskell, this is called a guard. Guards have the syntax | <boolean condition> = <body expression>
, and are evaluated in the order in which they are defined. Whenever the first boolean condition is true, the body expression is returned.
9. Solve exercise 6. in a different way. (&&
may be helpful in boolean conditions).
10. What is the type of [1,2,3]
?
- Is
1:[2,3]
the same thing as1:2:3:[]
? - Is
1:(2:[])
the same thing as(1:2):[]
?
11. Solve exercise 6 in a different way. (the function sort
may be helpful, as well as the functions head
, tail
and reverse
. Use :t
on each one, and test them).
12. Implement a list reversal function.
13. Write a function which extracts the third to last number from a list and returns True
, if that number is odd (hint: the function mod
may be useful)
V f [3,4,5,2,3,9] = False f [3,4,2,1,4,4] = True
14. Implement a function which returns the sum of integers from a list.
15. Implement a function which takes a list of booleans and returns false if at least one boolean from the list is false.
16. Implement a function which filters out all odd numbers from a list.
17. 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]
.
18. Sometimes it is helpful to define auxiliary functions which are not used later in our program. For instance:
f :: [[Integer]] -> [Bool] f [] = [] f l = (g (head l)):(f (tail l)) where g [] = True g l = h (tail l) h [] = True h l = False
What does the previous function do? Test it.
19. Implement a function which takes a list of booleans and returns the sum of True
values from the list. Example f [False,True,False,False,True] = 2
.
20. Implement insertion sort.
inssort :: [Integer] -> [Integer] inssort [] = inssort l =