Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 2. 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: <code haskell> f [] = ... f (h:t) = ... </code> ''(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? <code haskell> f (x:y:z:w:l) = w f _ = 0 </code> How about: <code haskell> f (x:y:z:w:l) = w </code> 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: <code haskell> f x y = (x,y) </code> 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: <code haskell> f 'a' _ = [] f x y = x:y </code> 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 be the function below: <code haskell> f "321CB" [("321CB", ["Matei", "Andrei", "Mihai"]), ("322CB",["George", "Matei"])] = ["Matei", "Mihai"] </code> 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: <code haskell> f x = (g.h) x where g x = 2*x h x = x + 1 </code> What type does f have? What type does g have? What does the following function do? <code haskell> f x = ff x where g x = 2*x h x = x + 1 ff = f.h </code> What does the following function do? <code haskell> f x = ff x where g x = 2*x h x = x + 1 ff = h.f </code> 30. Rewrite exercise 28 via function composition: <code haskell> f l = </code> 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: <code haskell> f [("Dan Matei Popovici",9),("Mihai",4),("Andrei Alex",6)] = [(["Dan", "Matei", "Popovici"],10),(["Andrei,Alex"],6)] </code> 32. Write the signature and implement the following function: <code haskell> f ["Matei", "Mihai"] ["Popovici","Dumitru"] = [("Matei","Popovici"), ("Mihai","Dumitru")] </code> 33. Implement the following function: <code haskell> f ["Matei", "Mihai"] ["Popovici","Dumitru"] = ["MPopovici", "MDumitru"] </code> 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 'f' y = 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: <code haskell> :t (+) :t (&&) :t (++) </code> 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. <code haskell> f l = (((++) "?").((:) '>')) l </code> How about? <code haskell> f = (((:) '>').(++"?")) </code> 38. Write a function of a single line, such that: <code haskell> f "Matei" = "{Matei}" </code> 39. Use only ''(:)'', ''reverse'' and functional composition ''.'' to solve exercise 38 40. What does the function ''($)'' to? (Use :t, and make up some tests) 41. Write the following implementation using only ($): <code haskell> f "||" "Matei" = "||Matei||" </code> 42. Solve exercise 13. from [[pp:l02|Lab 2]] using ''$'' (''++'' and ''reverse'' are also permitted) and other improvements (treat the case when the list has fewer elements):