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:
f l = head (tail (tail (tail l))))
22. Write the same function which returns 0 if the list contains less that four elements.
f [] = 0 f [_] = 0 f [_,_] = 0 f [_,_,_] = 0 f l = head (tail (tail (tail l))))
23. In Haskell, one can use patterns to examine the contents of lists, pairs and other dataypes.
f (x:y:z:w:l) = w f _ = 0
What types have x,y,z,w ? What type does have l ?
What about:
f (x:y:z:w:l) = w
What is the difference?
24. Write a function which filters out all negative numbers:
f [] = [] f (x:xs) =
| x > 0 = x:(f xs) | otherwise = f xs
25. What is the type of the function:
f x y = (x,y)
discussion on pairs f :: a → b → (a,b)
26. What is the type of the function:
f 'a' _ = [] f x y = x:y
discussion on strings f :: Char → String → String
27. Let f “321CB” [(“321CB”, [“Matei”, “Andrei”, “Mihai”]), (“322CB”,[“George, Matei”])]
= ["Matei", "Mihai"]
What is the signature of f? f :: String → [(String,[String])] → [String]
What does f do?
Write an implementation for f:
f x [] = [] f x 1)
where g (x:y:z:_) = | z > 0 = True | otherwise = False g _ = False
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 = (g . reverse . sort) l
where ...
31.(hard) Write a function which takes a list of pairs - student-name and grade, removes
those with grades <5, splits the name in substrings, and adds one point bonus to people with three names:
f [("Dan Matei Popovici",9),("Mihai",4),("Andrei Alex",6)] = [(["Dan", "Matei", "Popovici"],10),(["Andrei,Alex"],6)]
f l = (bonus . splall . rem) l
where rem [] = [] rem ((s,x):xs) | x < 5 = rem xs | otherwise = (s,x):(rem xs)
spl [] [] rest = rest spl [] w rest = w:rest spl (x:xs) w rest | x == ' ' = spl xs [] (w:rest) | otherwise = spl xs (x:w) rest
splall [] = []
splall 2) l
How about?
f = 3)
38. Write a function of a single line, such that:
f “Matei” = “[Matei]”
f = (“[”++).(++“]”)
39. Use only (:) to solve exercise 38
f = ((:) '[') . reverse . ((:) ']') . reverse
40. What does the function ($) to?
(Use :t, and make up some tests)
41. Write the following implementation using only ($):
f “||” “Matei” = “||Matei||”
f s l = (s++) $ h $ (++s) $ reverse l
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
| x == y = getM l | otherwise = f x ys where getM [] = [] getM ((x:l):xs) | x == 'M' = (x:l):(getM xs) | otherwise = getM xs28. Write a function which returns true if the third largest element from a list is positive f l = g (reverse (sort l
f ["Matei", "Mihai"] ["Popovici"] ["Dumitru"] = [("Matei","Popovici"), ("Mihai","Dumitru")]
f :: [String] -> [String] -> [(String,String)] f (x:xs) (y:ys) = (x,y):(f xs ys)33. Implement the following function:
f ["Matei", "Mihai"] ["Popovici"] ["Dumitru"] = ["MPopovici", "MDumitru"]
f (((c:_):xs)) (y:ys) = (c:y):(f xs ys)34. Sometimes it helps to use functions in infix form. For instance:
instead of mod x 2, we can write x 0109ode00322. We can also define functions infix:
x 01020032y = x + y
Implement an abbreviation function for exercise 31 and use it infix:
f (x:ys) (y:ys) = (x 0099onc0032y):(f xs ys) where (x:_) 0099onc0032s = x:s35. Just in the same way, we can treat infix functions as prefix.
:t (+) :t (&&) :t (++)36. What about function composition? Is it a special operator, or is it just a function as well?
:t (.)37. What does the function below do? f l = (((++) “?”).((:) '>'