Differences

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

Link to this comparison view

pp:scalalab3 [2022/05/25 01:11]
vbadoiu created
pp:scalalab3 [2022/05/25 01:15] (current)
vbadoiu
Line 1: Line 1:
-====== Lab 06. Polymorphism in Scala ======+====== Lab 11. Polymorphism ​and For expressions ​in Scala ====== 
 + 
 +=== I. Polymorphism ​===
  
 This lab will start with the implementations discussed during lecture. Please find the polymorphic trait ''​FList[A]''​ below: This lab will start with the implementations discussed during lecture. Please find the polymorphic trait ''​FList[A]''​ below:
Line 47: Line 49:
   * Can you figure which ones are best implemented in the trait and which in the case classes?   * Can you figure which ones are best implemented in the trait and which in the case classes?
  
-**6.1.** ''​indexOf''​ determines the position of a value in the list (starting with 0)+**1.1.** ''​indexOf''​ determines the position of a value in the list (starting with 0)
 <code scala> <code scala>
 def indexOf(e: A): Int def indexOf(e: A): Int
 </​code>​ </​code>​
  
-**6.2.** ''​update''​ creates a new list where the given position is modified with a new value:+**1.2.** ''​update''​ creates a new list where the given position is modified with a new value:
 <code scala> <code scala>
 //​Cons(1,​Cons(2,​Cons(3,​FNil()))).update(9,​1) = Cons(1,​Cons(9,​Cons(3,​FNil()))) //​Cons(1,​Cons(2,​Cons(3,​FNil()))).update(9,​1) = Cons(1,​Cons(9,​Cons(3,​FNil())))
Line 58: Line 60:
 </​code>​ </​code>​
  
-**6.3.** ''​append''​ concatenates this list to another:+**1.3.** ''​append''​ concatenates this list to another:
 <code scala> <code scala>
 def append(l: FList[A]): FList[A] def append(l: FList[A]): FList[A]
 </​code>​ </​code>​
  
-**6.4.** ''​reverse''​ returns the reversed list:+**1.4.** ''​reverse''​ returns the reversed list:
 <code scala> <code scala>
 def reverse: FList[A] def reverse: FList[A]
 </​code>​ </​code>​
  
-**6.5.** ''​last''​ returns the last element of the list:+**1.5.** ''​last''​ returns the last element of the list:
 <code scala> <code scala>
 def last: A def last: A
 </​code>​ </​code>​
  
-**6.6.** ''​filter''​ filters the elements of the list:+**1.6.** ''​filter''​ filters the elements of the list:
 <code scala> <code scala>
 def filter(p: A => Boolean): FList[A] def filter(p: A => Boolean): FList[A]
 </​code>​ </​code>​
  
-**6.7.** ''​zip''​ combines two lists into a list of pairs. If **either** list is larger, the remaining elements are discarded.+**1.7.** ''​zip''​ combines two lists into a list of pairs. If **either** list is larger, the remaining elements are discarded.
 <code scala> <code scala>
 // Cons(1,​(Cons(2,​Cons(3,​FNil()))).zip(Cons(true,​Cons(false,​Cons(true,​FNil())))) = // Cons(1,​(Cons(2,​Cons(3,​FNil()))).zip(Cons(true,​Cons(false,​Cons(true,​FNil())))) =
Line 85: Line 87:
 </​code>​ </​code>​
  
-**6.8.** ''​insSorted''​ inserts an element into a sorted list so that the result is a sorted list.+**1.8.** ''​insSorted''​ inserts an element into a sorted list so that the result is a sorted list.
 <code scala> <code scala>
 def insSorted(f:​ A => Int)(e: A): FList[A] def insSorted(f:​ A => Int)(e: A): FList[A]
 </​code>​ </​code>​
  
-**6.9.** ''​sortBy''​ sorts a list using insertion sort.+**1.9.** ''​sortBy''​ sorts a list using insertion sort.
 <code scala> <code scala>
 def sortBy(f: A => Int): FList[A] def sortBy(f: A => Int): FList[A]
 </​code>​ </​code>​
  
-**6.10 (!)** Implement a method ''​pack''​ which encodes a sorted list as follows:+**1.10 (!)** Implement a method ''​pack''​ which encodes a sorted list as follows:
 <code scala> <code scala>
 [1,​1,​1,​2,​3,​4,​4,​5,​6].pack = [(1,​3),​(2,​1),​(3,​1),​(4,​2),​(5,​1),​(6,​1)] [1,​1,​1,​2,​3,​4,​4,​5,​6].pack = [(1,​3),​(2,​1),​(3,​1),​(4,​2),​(5,​1),​(6,​1)]
Line 101: Line 103:
 </​code>​ </​code>​
  
-====== L07. For expressions ​======+=== II. For expressions ===
  
-In this lab, we will use matrices to encode Bitmap images. The format is called BPM, and more details are available [[https://​en.wikipedia.org/​wiki/​Netpbm#​File_formats|here]]. Our format will be grayscale only. Each pixel of the matrix is encoded as an integer, with values from 0 to 255. Some examples are shown below:+We will use matrices to encode Bitmap images. The format is called BPM, and more details are available [[https://​en.wikipedia.org/​wiki/​Netpbm#​File_formats|here]]. Our format will be grayscale only. Each pixel of the matrix is encoded as an integer, with values from 0 to 255. Some examples are shown below:
  
 <​code>​ <​code>​
Line 135: Line 137:
 </​code> ​ </​code> ​
  
-**7.1.** Write a function which converts an image to a string (Hint: you can draw inspiration from a similar one from the lecture):+**2.1.** Write a function which converts an image to a string (Hint: you can draw inspiration from a similar one from the lecture):
 <code scala> <code scala>
 def show(m: Img): String = ??? def show(m: Img): String = ???
 </​code>​ </​code>​
  
-**7.2.** Write a function which performs a horizontal flip on an image. Try to first visualise (you may use pen and paper) how the transformation would look like.+**2.2.** Write a function which performs a horizontal flip on an image. Try to first visualise (you may use pen and paper) how the transformation would look like.
 <code scala> <code scala>
 def hFlip(img: Img): Img = ??? def hFlip(img: Img): Img = ???
 </​code>​ </​code>​
  
-**7.3.** Write a function which performs vertical flip.+**2.3.** Write a function which performs vertical flip.
 <code scala> <code scala>
 def vFlip(img: Img): Img = ??? def vFlip(img: Img): Img = ???
 </​code> ​ </​code> ​
  
-**7.4.**  Write a function which performs a 90 degrees rotation to the right. (Hint: you need an ingredient from the lecture. Also, note that there are multiple possible implementations.)+**2.4.**  Write a function which performs a 90 degrees rotation to the right. (Hint: you need an ingredient from the lecture. Also, note that there are multiple possible implementations.)
 <code scala> <code scala>
 def rot90Right(img:​ Img): Img = ??? def rot90Right(img:​ Img): Img = ???
 </​code>​ </​code>​
  
-**7.5.** Write a function which performs a 90 degrees rotation to the left.+**2.5.** Write a function which performs a 90 degrees rotation to the left.
 <code scala> <code scala>
 def rot90Left(img:​ Img): Img = ??? def rot90Left(img:​ Img): Img = ???
 </​code>​ </​code>​
  
-**7.6.** Write a function which inverts an image (values 0 become 255, 1 - 254, and so forth).+**2.6.** Write a function which inverts an image (values 0 become 255, 1 - 254, and so forth).
  
-**7.7.** Write a function which crops a given image, using two, two-dimensional coordinates:​ the higher-left point x and y, and the lower-right point x and y. An example is shown below:+**2.7.** Write a function which crops a given image, using two, two-dimensional coordinates:​ the higher-left point x and y, and the lower-right point x and y. An example is shown below:
 <code scala> <code scala>
 val img = List(List(0,​0,​1,​0,​0),​ List(0,​1,​0,​1,​0),​ List(0,​1,​1,​1,​0),​ List(1,​0,​0,​0,​1),​ List(1,​0,​0,​0,​1)) val img = List(List(0,​0,​1,​0,​0),​ List(0,​1,​0,​1,​0),​ List(0,​1,​1,​1,​0),​ List(1,​0,​0,​0,​1),​ List(1,​0,​0,​0,​1))
Line 177: Line 179:
 </​code> ​ </​code> ​
  
-**7.8.** Write a function which returns a list of all positions which have pixels of larger intensity than x+**2.8.** Write a function which returns a list of all positions which have pixels of larger intensity than x
 <code scala> <code scala>
 def largerPos(img:​ Img, int: Int): List[(Int,​Int)] = ??? def largerPos(img:​ Img, int: Int): List[(Int,​Int)] = ???
 </​code>​ </​code>​
  
-**7.9.** Write a function which adds ''​x''​ to the intensity of each pixel.+**2.9.** Write a function which adds ''​x''​ to the intensity of each pixel.
 <code scala> <code scala>
 def contrast(x: Int)(img: Img): Img = ??? def contrast(x: Int)(img: Img): Img = ???
 </​code>​ </​code>​
  
-**7.10.** Write a function which takes two images ''​X''​ and ''​Y''​ and //glues// them on the horizontal axis (the resulting image will be ''​XY''​)+**2.10.** Write a function which takes two images ''​X''​ and ''​Y''​ and //glues// them on the horizontal axis (the resulting image will be ''​XY''​)
 <code scala> <code scala>
 def hglue(img1: Img, img2: Img): Img = ??? def hglue(img1: Img, img2: Img): Img = ???
 </​code>​ </​code>​
  
-**7.11.** Write a function which takes two images ''​X''​ and ''​Y''​ and //glues// them on the vertical axis:+**2.11.** Write a function which takes two images ''​X''​ and ''​Y''​ and //glues// them on the vertical axis:
 <code scala> <code scala>
 def vglue(img1: Img, img2: Img): Img = ??? def vglue(img1: Img, img2: Img): Img = ???
 </​code>​ </​code>​
  
-**7.12.** Define a function that takes a **square** image, and draws two diagonal lines of intensity 1 across it. Use ''​_.until(_)''​ and ''​_.toList''​.+**2.12.** Define a function that takes a **square** image, and draws two diagonal lines of intensity 1 across it. Use ''​_.until(_)''​ and ''​_.toList''​.
 <code scala> <code scala>
 def diag(img: Img): Img = ??? def diag(img: Img): Img = ???
 </​code>​ </​code>​
  
-**7.13.** Define a function which blurs an image as follows:+**2.13.** Define a function which blurs an image as follows:
   * for each pixel p of intensity > 1: make all neighbouring pixels of intensity 0 equal to p-1 (including diagonals).   * for each pixel p of intensity > 1: make all neighbouring pixels of intensity 0 equal to p-1 (including diagonals).
   * if some pixel of intensity 0 is in the vicinity of two different >1 pixels of different intensities,​ the one which is larger should be taken into account   * if some pixel of intensity 0 is in the vicinity of two different >1 pixels of different intensities,​ the one which is larger should be taken into account
Line 211: Line 213:
 </​code> ​ </​code> ​
  
-**7.14. (!) ** Define a function which builds an effect of intensity x as shown below:+**2.14. (!) ** Define a function which builds an effect of intensity x as shown below:
 <code scala> <code scala>
 val img2 = List( val img2 = List(