Я хочу написать функцию, которая помещает Список.
object Flat {
def flatten[T](list: List[T]): List[T] = list match {
case Nil => Nil
case head :: Nil => List(head)
case head :: tail => (head match {
case l: List[T] => flatten(l)
case i => List(i)
}) ::: flatten(tail)
}
}
object Main {
def main(args: Array[String]) = {
println(Flat.flatten(List(List(1, 1), 2, List(3, List(5, 8)))))
}
}
Я не знаю, почему он не работает, он возвращает List(1, 1, 2, List(3, List(5, 8)))
, но он должен быть List(1, 1, 2, 3, 5, 8)
.
Можете ли вы дать мне подсказку?