This is an old revision of the document!
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 General matrix
Given the following implementation of a generalized matrix, solve the next few exercices.
trait GMatrix case class Scalar(value: Int) extends GMatrix case class Vector(values: List[Int]) extends GMatrix case class Matrix(values: List[List[Int]]) extends GMatrix
4.3.1 Write a function which takes a GMatrix and multiplies each element by an Int.
def multiply(m: GMatrix, x: Int): GMatrix = ???
4.3.1 Write a function which takes a two GMatrices and multiplies them
def multiply(m1: GMatrix, m2: GMatrix): GMatrix = ???
4.4 Human Genome
Given the following implementation of a human genome, solve the next few exercices.
trait Gene case class A(code: Int) extends Gene case class C(code: Int) extends Gene case class G(code: Int) extends Gene case class T(code: Int) extends Gene
4.4.1 Write a function which takes a two gene sequencies and checks their compatibility. A-T, C-G, T-A, G-C
def compatibility(seq1: List[Gene], seq2: List[Gene]): Boolean = ???
4.4.2 Write a function which takes a gene sequence and checks their encoded value (G1G2G3 → code1code2code3)
def encode(seq: List[Gene]): List[Int]= ???
4.4.2 Write a function which takes a gene sequence and returns the gene sequence that annihilates given one (make the wrong combinations, unlike 4.4.1)
def worstEnemy(seq: List[Gene]): List[Gene]= ???