Lab 4. Data types in Scala

Objectives:

  • get familiar with algebraic data types
  • get familiar with pattern matching and recursion with them

4.1 Natural Numbers

Given the following implementation of the natural numbers, solve the next few exercices.

trait NaturalNumber
case object Zero extends NaturalNumber
case class Successor(x: NaturalNumber) extends NaturalNumber

4.1.1 Write a function which takes two natural numbers, and return their sum.

def add(x: NaturalNumber, y: NaturalNumber): NaturalNumber = ???

4.1.2 Write a function which takes two natural numbers, and return their product.

def multiply(x: NaturalNumber, y: NaturalNumber): NaturalNumber = ???

4.1.3 Write a function which takes an int and converts it to a NaturalNumber.

def toNaturalNumber(x: Int): NaturalNumber = ???

4.2 Binary Trees

Given the following implementation of binary trees, solve the next few exercices.

trait BTree
case object EmptyTree extends BTree
case class Node(value: Int, left: BTree, right: BTree) extends BTree

4.2.1 Write a function which takes a BinaryTree and returns its depth.

def depth(tree: BTree): Int = ???

4.2.2 Write a function which takes a BinaryTree and returns the number of nodes with even number of children.

def evenChildCount(tree: BTree): Int = ???

4.2.3 Write a function which takes a BinaryTree and flattens it (turns it into a list containing the values of the nodes).

def flatten(tree: BTree): List[Int] = ???

4.2.4 Write a function which takes a BinaryTree and return the number of nodes whose values follow a ceratain rule.

def countNodes(tree: BTree, cond: Int => Boolean): Int = ???

4.2.5 Write a function which takes a BinaryTree and return mirrored BTree.

def mirror(tree: BTree): BTree= ???

4.3 Matrix manipulation

We shall represent matrices as lists of lists, i.e. values of type [ [Integer ] ]. Each element in the outer list represents a line of the matrix. Hence, the matrix

$ \displaystyle \left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}\right)$

will be represented by the list [ [1,2,3],[4,5,6],[7,8,9] ].

To make signatures more legible, add the type alias to your code:

 type Matrix = List[List[Int]] 

which makes the type-name Matrix stand for [ [Integer] ].

4.3.1 Write a function that computes the scalar product with an integer:

$ \displaystyle 2 * \left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}\right) = \left(\begin{array}{ccc} 2 & 4 & 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18 \\ \end{array}\right)$

def vprod(m: Matrix)(v: Int): Matrix = ??? 

4.3.2 Write a function which adjoins two matrices by extending rows:

$ \displaystyle \left(\begin{array}{cc} 1 & 2 \\ 3 & 4\\\end{array}\right) hjoin \left(\begin{array}{cc} 5 & 6 \\ 7 & 8\\\end{array}\right) = \left(\begin{array}{cc} 1 & 2 & 5 & 6 \\ 3 & 4 & 7 & 8\\\end{array}\right) $

def join(m1: Matrix, m2: Matrix): Matrix = ???

4.3.3 Write a function which adjoins two matrices by adding new rows:

$ \displaystyle \left(\begin{array}{cc} 1 & 2 \\ 3 & 4\\\end{array}\right) vjoin \left(\begin{array}{cc} 5 & 6 \\ 7 & 8\\\end{array}\right) = \left(\begin{array}{cc} 1 & 2 \\ 3 & 4 \\ 5 & 6\\ 7 & 8\\ \end{array}\right) $

def vjoin(m1: Matrix, m2: Matrix): Matrix = ???

4.3.4 Write a function which adds two matrices.

def msum(m1: Matrix, m2: Matrix): Matrix = ???

4.3.5 Write a function which computes the transposition of a matrix:

$ tr \left(\begin{array}{ccc} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{array}\right) = \left(\begin{array}{ccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \\ \end{array}\right) $

def tr(m: Matrix): Matrix = ???

4.3.6 Write a function which computes the vectorial product of two matrices.

  • (Hint: start by writing a function which computes $ a_{ij}$ for a given line $ i$ and column $ j$ (both represented as lists))
  • (Hint: write a function which takes a line of matrix m1 and the matrix m2 and computes the respective line from the product)
def mprod(m1: Matrix, m2: Matrix): Matrix = ???