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. ===== 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):