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
fp:lab01-java-intro [2021/03/11 13:17]
pdmatei
fp:lab01-java-intro [2021/03/11 14:50] (current)
dmihai
Line 3: Line 3:
 ===== 1. A single function to implement all of them ===== ===== 1. A single function to implement all of them =====
  
-1.1. Write a method that takes a list of integers and returns a new list with all elements multiplied by two.+1.1. Write a method that takes a list of **integers** and returns a new list with all elements multiplied by two.
  
-1.2. Write a method that takes a list of doubles and returns a new list with all elements multiplied by two.+1.2. Write a method that takes a list of **doubles** and returns a new list with all elements multiplied by two.
  
 1.3. Write a method that takes a list of integers and returns a new list with all the elements’ successors (i.e. the elements incremented by 1). 1.3. Write a method that takes a list of integers and returns a new list with all the elements’ successors (i.e. the elements incremented by 1).
Line 15: Line 15:
 1.6. (Discussion) How can we pass **functions** as parameter (to another function) in Java? Consider **unary** functions of the form: $math[f : A \rightarrow B] (where $math[A] and $math[B] are generic). 1.6. (Discussion) How can we pass **functions** as parameter (to another function) in Java? Consider **unary** functions of the form: $math[f : A \rightarrow B] (where $math[A] and $math[B] are generic).
  
-1.7. (!) Write an implementation for ''​map''​.+1.7. (!) Write an implementation for ''​map'' ​and use ''​map''​ to solve 1.4.
  
  
Line 26: Line 26:
 2.3. Write a method that takes a list of integers and returns a new list containing **even** elements **larger** than 100. 2.3. Write a method that takes a list of integers and returns a new list containing **even** elements **larger** than 100.
  
- d. Write a method that takes a list of integers ​and returns a new list of the elements ​which are prime+2.4. Write a method that takes a list of strings ​and returns a new list of strings ​which start with letter ''​B''​ only.
  
-2.4. (!) Write the signature of a generic ''​filter''​ function over lists.+2.5. (!) Write the signature of a generic ''​filter''​ function over lists.
  
-2.5. (!!) Implement ''​filter''​.+2.6. (!!) Implement ''​filter''​.
  
 +===== 3. Reducing lists =====
 +
 +3.1. Write a method that takes a list of integers and returns the sum of all the elements.
 +
 +3.2. Write a method that takes a list of integers and returns the product of all the elements.
 +
 +3.3. Write a method that takes a list of integers and returns a string representation of all elements separated by commas, e.g. “1,​2,​3,​4”
 +
 +3.4. Write a method that takes a list of integers and returns their greatest common denominator.
 +
 +3.5. (Discussion) What is (could be!) common among all implementations?​ How should a ''​fold''​ function **signature** (which generalises over all previous ones) look like?
 +
 +3.6. (!) Implement ''​fold''​ with all its prerequisites and use it to solve 3.4.
 +
 +===== 4. Zipping lists =====
 +
 +4.1. Write a method that takes two lists and returns a new list of all pairwise products. E.g. ''​[2,​ 4, 2''​] and ''​[6,​ 7, 8]''​ should yield ''​[12,​ 28, 16]''​
 +
 +4.2. Write a method that takes two lists and returns a new list of all pairwise sums. E.g. ''​[2,​ 4, 2]''​ and ''​[6,​ 7, 8]''​ should yield ''​[8,​ 11, 10]''​
 +
 +4.3. Write a method that takes a list and returns a new list with the square of each element.
 +
 +4.4. (!) Identify a generic function which can replace all the above, and implement it. Solve 4.3. using it.