added assignment 7 ex 5

This commit is contained in:
Louis Heredero 2025-04-16 10:26:44 +02:00
parent 95fdbf7abf
commit e58c399bad
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

26
src/Assignment7/Ex5.sc Normal file
View File

@ -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