Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
pp:l02 [2020/03/12 15:19]
pdmatei
pp:l02 [2021/03/16 19:24] (current)
roxana_elena.stiuca [2.3. Strings in Haskell]
Line 1: Line 1:
-====== Pattern matching and basic types in Haskell ======+====== ​2. Pattern matching and basic types in Haskell ======
  
-21. Extract the fourth element from a list of integers +===== 2.1. Pattern matching =====
- +
-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: 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:
Line 16: Line 12:
 ''​(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: ''​(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?+2.1.1. Write a function which returns the number of elements from a list. Use patterns. 
 + 
 +2.1.2. Write a function which takes a list of integer lists, and concatenates them. (E.g. ''​f [ [1,​2,​3],​[4,​5] ] = [1,​2,​3,​4,​5]''​). 
 + 
 +2.1.3 (!) Write the **same** function, this time using only the **cons** ('':''​) operator as well as **patterns**.  
 + 
 +2.1.4. Write a function which removes duplicates from a list of integers. 
 + 
 +===== 2.2. Types in Haskell ===== 
 + 
 +2.2.1. What are the types of ''​x,​y,​z,​w''​ and that of ''​l''​ in the implementation below?
 <code haskell> <code haskell>
 f (x:y:z:w:l) = w f (x:y:z:w:l) = w
Line 28: Line 34:
 What is the difference? What is the difference?
     ​     ​
- +2.2.2. What is the type of the function:
-24Write a function which filters out all negative numbers from a listUse patterns. +
- +
-25. What is the type of the function:+
 <code haskell> <code haskell>
 f x y = (x,y) f x y = (x,y)
Line 38: Line 41:
 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.  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:+2.2.3. What is the type of the function:
 <code haskell> <code haskell>
 f '​a'​ _ = [] f '​a'​ _ = []
Line 46: Line 49:
 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. 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"​])] +2.2.4. Let f be the function below: 
-  ​= ["​Matei",​ "​Mihai"​]+<code haskell>​ 
 +    ​f "​321CB"​ [("​321CB",​ ["​Matei",​ "​Andrei",​ "​Mihai"​]),​ ("​322CB",​["​George""Matei"​])] = ["​Matei",​ "​Mihai"​] 
 +</​code>​
  
-What is the signature of f? +What is the signature of ''​f''​?
- f :: String -> [(String,​[String])] -> [String]+
  
-What does f do (note that ''​Matei''​ and ''​Mihai''​ both start with letter '​M'​)?​ 
  
-Write an implementation for f.+===== 2.3. Strings in Haskell =====
  
-28. Write a function which returns ''​True''​ if the third largest element from a list is positive+2.3.1. Write a function which takes a list of words and makes the first letter of each word uppercase.
  
-===== Function composition and function ​application =====+2.3.2. Write a function ​which takes a list of words and makes **all** letters uppercase.
  
-29Sometimes it really helps to define function via function ​compositionFor instance+2.3.3. (!) Write a function ​which takes a text and a pattern and returns the number of occurrences of the pattern in the text 
-<code haskell>​ +Example
-f x (g.h) x +<code haskell>  
- where g x = 2*x +search "I eat green apples"​ "​eat" ​1 
-       h x = x + 1+search "​ababaab"​ "​aba" ​= 2
 </​code>​ </​code>​
  
-What type does f have? +2.3.4. (!) What does ''​f'',​ defined in exercise ​2.2.4., do (note that ''​Matei''​ and ''​Mihai''​ both start with letter '​M'​)? Write an implementation for ''​f''​.
-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.+
-</​code>​ +
-What does the following function do? +
-<code haskell>​ +
-x = ff x +
- where g x = 2*x +
-       h x = x + 1 +
-       ff = h.+
-</​code>​ +
- +
-30Rewrite exercise 28 via function composition:​ +
-<code haskell>​ +
-f l =  +
-</​code>​ +
- +
-31Write a function which takes a list of pairs - student-name and graderemoves 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:+2.3.5. Write the signature and implement the following function:
 <code haskell> <code haskell>
    f ["​Matei",​ "​Mihai"​] ["​Popovici","​Dumitru"​] = [("​Matei","​Popovici"​),​ ("​Mihai","​Dumitru"​)]    f ["​Matei",​ "​Mihai"​] ["​Popovici","​Dumitru"​] = [("​Matei","​Popovici"​),​ ("​Mihai","​Dumitru"​)]
 </​code>​ </​code>​
  
-33. Implement the following function:+2.3.6. Implement the following function:
 <code haskell> <code haskell>
    f ["​Matei",​ "​Mihai"​] ["​Popovici","​Dumitru"​] = ["​MPopovici",​ "​MDumitru"​]    f ["​Matei",​ "​Mihai"​] ["​Popovici","​Dumitru"​] = ["​MPopovici",​ "​MDumitru"​]
 </​code>​ </​code>​
  
-34Sometimes it helps to use functions in infix formFor instance, instead ​of ''​mod x 2''​we can write ''​x `mod` 2''​We can also define infix functions:+2.3.7. Write a function which takes a list of pairs - student-name and graderemoves those with grades less than 5, splits the name in substrings, and adds one bonus point to people with three namesExample:
 <code haskell> <code haskell>
- x `f` y = x + y +    ​[("Dan Matei Popovici"​,9),("​Mihai"​,4),("Andrei Alex",6)] = 
-</​code>​ +     [(["Dan", "​Matei"​"Popovici"],10),(["Andrei""Alex"],6)]
- +
-Implement a function for transforming lists into pairsin exercise 32. and use it in the infix form. +
- +
-35. Just in the same waywe 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 operatoror is it just a function as well? What does '':​t (.)''​ do? +
- +
-37. What does the function below do? Take parts of the function belowand test them. +
-<code haskell>​ +
-f l = (((++) "?").((:) '>'​)) l +
-</​code>​ +
- +
-How about? +
-<code haskell>​ +
-f = (((:) '>'​).(++"?")) +
-</​code>​ +
- +
-38. Write a function of a single linesuch that: +
-<code haskell>​ +
-"​Matei" ​"{Matei}" +
-</​code>​ +
- +
-39. Use only ''​(:​)''​ to solve exercise 38  +
- +
-40. What does the function ''​($)''​ to?  +
-    (Use :tand make up some tests) +
- +
-41. Write the following implementation using only ($): +
-<code haskell>​ +
-"||" "Matei" = "​||Matei||"+
 </​code>​ </​code>​
  
  
  
-42.  Solve exercise 13. from [[Lab 2 | 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 `mod` 2  
-   ​  ​ g _ = False