Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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. <code scala> trait NaturalNumber case object Zero extends NaturalNumber case class Successor(x: NaturalNumber) extends NaturalNumber </code> **4.1.1** Write a function which takes two natural numbers, and return their sum. <code scala> def add(x: NaturalNumber, y: NaturalNumber): NaturalNumber = ??? </code> **4.1.2** Write a function which takes two natural numbers, and return their product. <code scala> def multiply(x: NaturalNumber, y: NaturalNumber): NaturalNumber = ??? </code> **4.1.3** Write a function which takes an int and converts it to a NaturalNumber. <code scala> def toNaturalNumber(x: Int): NaturalNumber = ??? </code> ==== 4.2 Binary Trees ==== Given the following implementation of binary trees, solve the next few exercices. <code scala> trait BTree case object EmptyTree extends BTree case class Node(value: Int, left: BTree, right: BTree) extends BTree </code> **4.2.1** Write a function which takes a BinaryTree and returns its depth. <code scala> def depth(tree: BTree): Int = ??? </code> **4.2.2** Write a function which takes a BinaryTree and returns the number of nodes with even number of children. <code scala> def evenChildCount(tree: BTree): Int = ??? </code> **4.2.3** Write a function which takes a BinaryTree and flattens it (turns it into a list containing the values of the nodes). <code scala> def flatten(tree: BTree): List[Int] = ??? </code> **4.2.4** Write a function which takes a BinaryTree and return the number of nodes whose values follow a ceratain rule. <code scala> def countNodes(tree: BTree, cond: Int => Boolean): Int = ??? </code> **4.2.5** Write a function which takes a BinaryTree and return mirrored BTree. <code scala> def mirror(tree: BTree): BTree= ??? </code> ==== 4.3 General matrix ==== Given the following implementation of a generalized matrix, solve the next few exercices. <code scala> 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 </code> **4.3.1** Write a function which takes a GMatrix and multiplies each element by an Int. <code scala> def multiply(m: GMatrix, x: Int): GMatrix = ??? </code> **4.3.1** Write a function which takes a two GMatrices and multiplies them <code scala> def multiply(m1: GMatrix, m2: GMatrix): GMatrix = ??? </code> ==== 4.4 Human Genome ==== Given the following implementation of a human genome, solve the next few exercices. <code scala> 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 </code> **4.4.1** Write a function which takes a two gene sequencies and checks their compatibility. ''A-T, C-G, T-A, G-C'' <code scala> def compatibility(seq1: List[Gene], seq2: List[Gene]): Boolean = ??? </code> **4.4.2** Write a function which takes a gene sequence and checks their encoded value (G1G2G3 -> code1code2code3) <code scala> def encode(seq: List[Gene]): List[Int]= ??? </code> **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) <code scala> def worstEnemy(seq: List[Gene]): List[Gene]= ??? </code>