diff --git a/src/Assignment7/Ex5.sc b/src/Assignment7/Ex5.sc new file mode 100644 index 0000000..635ee5c --- /dev/null +++ b/src/Assignment7/Ex5.sc @@ -0,0 +1,26 @@ +/* +1 +1 1 +2 1 +1 2 1 1 +1 1 1 2 2 1 +3 1 2 2 1 1 +Next : +1 3 1 1 2 2 2 1 + */ + +def nextLine(current: List[Int]) : List[Int] = { + current.foldRight(List.empty[(Int, Int)])((x, acc) => { + (x, acc) match { + case (a, p :: rest) if a == p._2 => (p._1 + 1, p._2) :: rest + case _ => (1, x) :: acc + } + }).flatten(p => List(p._1, p._2)) +} + +def makeSequence(start: List[Int]): LazyList[List[Int]] = { + start #:: makeSequence(nextLine(start)) +} + +lazy val sequence: LazyList[List[Int]] = makeSequence(List(1)) +sequence.take(7).toList