Read the common instructions first if you have not read them.
sbt new kaist-plrg-cs320/scala-tutorial.g8
volumeOfCuboid
takes three non-negative integersa
,b
, andc
denoting the lengths of three sides and returns the volume of the cuboid.
test(volumeOfCuboid(1, 3, 5), 15)
test(volumeOfCuboid(2, 3, 4), 24)
concat
takes two stringsx
andy
and returns their concatenation.
test(concat("x", "y"), "xy")
test(concat("abc", "def"), "abcdef")
addN
takes an integern
and returns a function that addsn
to a given integer.
test(addN(5)(3), 8)
test(addN(5)(42), 47)
twice
takes a functionf
whose type isInt => Int
and returns a function that appliesf
twice.
test(twice(addN(3))(2), 8)
test(twice(addN(3))(7), 13)
compose
takes twoInt => Int
functionsf
andg
and returns their compositionf ∘ g
.
test(compose(addN(3), addN(4))(5), 12)
test(compose(addN(3), addN(4))(11), 18)
double
takes an interger listl
and returns a list whose elements are the doubles of the elements ofl
.
test(double(List(1, 2, 3)), List(2, 4, 6))
test(double(double(List(1, 2, 3, 4, 5))), List(4, 8, 12, 16, 20))
sum
takes an integer listl
and returns the sum of the elements ofl
.
test(sum(List(1, 2, 3)), 6)
test(sum(List(4, 2, 3, 7, 5)), 21)
getKey
takes a mapm
from strings to integers and a strings
and returns the integer corresponding tos
ifm
has such mapping and throws an exception with a message containings
otherwise.
val m: Map[String, Int] = Map("Ryu" -> 42, "PL" -> 37)
test(getKey(m, "Ryu"), 42)
testExc(getKey(m, "CS320"), "CS320")
The Tree
type represents binary trees.
A Tree
is either Branch
(a non-leaf node) or Leaf
(a leaf node).
A Branch
consists of three fields: left
and right
denoting
the left and right subtrees and value
denoting its value.
A Leaf
has one field: value
denoting its value.
sealed trait Tree
case class Branch(left: Tree, value: Int, right: Tree) extends Tree
case class Leaf(value: Int) extends Tree
The Tree
type is already defined in Template.scala
.
DO NOT define it again in Implementation.scala
.
countLeaves
takes a treet
and and returns the number of its leaf nodes.
val t1: Tree = Branch(Leaf(1), 2, Leaf(3))
val t2: Tree = Branch(Leaf(1), 2, Branch(Leaf(3), 4, Leaf(5)))
test(countLeaves(t1), 2)
test(countLeaves(t2), 3)
flatten
takes a treet
and returns a list containing the value of each node int
with in-order traversal.
val t1: Tree = Branch(Leaf(1), 2, Leaf(3))
val t2: Tree = Branch(Leaf(1), 2, Branch(Leaf(3), 4, Leaf(5)))
test(flatten(t1), List(1, 2, 3))
test(flatten(t2), List(1, 2, 3, 4, 5))