This is an old revision of the document!
Pattern matching and basic types in Haskell
21. Extract the fourth element from a list of integers
22. Write the same function which returns 0 if the list contains less that four elements and 0 otherwise.
Pattern matching
It is likely that in the above implementations you used head
and tail
, or branch definitions combined with '_' to solve the exercises. Haskell supports a more elegant means of value introspection, called pattern matching. For instance, in:
f [] = ... f (h:t) = ...
(h:t)
is a pattern which denotes a non-empty list where h
is the first element and t
is the rest of the list. In fact, []
is also a pattern denoting the empty lists. Patterns are allways surrounded by round parentheses. They can also be composite, as in the exercise below:
23. What are the types of x,y,z,w
and that of l
in the implementation below?
f (x:y:z:w:l) = w f _ = 0
How about:
f (x:y:z:w:l) = w
What is the difference?
24. Write a function which filters out all negative numbers from a list. Use patterns.
25. What is the type of the function:
f x y = (x,y)
In the body of a function definition, ,
is a base constructor for pairs. The following are pairs: (1,2), (“Matei”,20), ([1,2],3)
. Note that there is no restriction on the type of the first and second values of a pair.
26. What is the type of the function:
f 'a' _ = [] f x y = x:y
In Haskell, the type String
is equivalent to [Char]
(hence strings are lists of chars). Strings can be introspected using patterns just like any other list.
27. Let f “321CB” [(“321CB”, [“Matei”, “Andrei”, “Mihai”]), (“322CB”,[“George, Matei”])] = [“Matei”, “Mihai”]
What is the signature of f
?
What does f
do (note that Matei
and Mihai
both start with letter 'M')?
Write an implementation for f
.
28. Write a function which returns True
if the third largest element from a list is positive
Function composition and function application
29. Sometimes it really helps to define function via function composition. For instance:
f x = (g.h) x where g x = 2*x h x = x + 1
What type does f have? What type does g have?
What does the following function do?
f x = ff x where g x = 2*x h x = x + 1 ff = f.h
What does the following function do?
f x = ff x where g x = 2*x h x = x + 1 ff = h.f
30. Rewrite exercise 28 via function composition:
f l =
31. Write a function which takes a list of pairs - student-name and grade, removes those with grades less than 5, splits the name in substrings, and adds one bonus point to people with three names. Example:
f [("Dan Matei Popovici",9),("Mihai",4),("Andrei Alex",6)] = [(["Dan", "Matei", "Popovici"],10),(["Andrei,Alex"],6)]
32. Write the signature and implement the following function:
f ["Matei", "Mihai"] ["Popovici","Dumitru"] = [("Matei","Popovici"), ("Mihai","Dumitru")]
33. Implement the following function:
f ["Matei", "Mihai"] ["Popovici","Dumitru"] = ["MPopovici", "MDumitru"]
34. Sometimes it helps to use functions in infix form. For instance, instead of mod x 2
, we can write x mod 2
. We can also define infix functions:
x 01020032y = x + y
Implement a function for transforming lists into pairs, in exercise 32. and use it in the infix form.
35. Just in the same way, we can treat infix functions as prefix. We do this using round parentheses. Test:
:t (+) :t (&&) :t (++)
36. What about function composition? Is it a special operator, or is it just a function as well? What does :t (.)
do?
37. What does the function below do? Take parts of the function below, and test them.
f l = (((++) "?").((:) '>')) l
How about?
f = (((:) '>').(++"?"))
38. Write a function of a single line, such that:
f "Matei" = "{Matei}"
39. Use only (:)
to solve exercise 38
40. What does the function ($)
to?
(Use :t, and make up some tests)
41. Write the following implementation using only ($):
f "||" "Matei" = "||Matei||"
42. Solve exercise 13. from pp:l02 using $ and other improvements (treat the case when the list has
fewer elements):
f l = g $ reverse l where g (_:_:x:_) = not $ x 0109od00322 g _ = False