flattening list of lists in Scala with out using flatten method giving bad result -


i tried flatten list of lists using below code. when put on paper, should work think misinterpreted or ignorant of how lists work. 1 tell me went wrong.

val = list(list(1,2,3),list(4,5,6),list(7,8,9))  def flatten(xss : list[list[any]]) : list[any] = {   def flat(xs : list[any]) : list[any] = xs match {     case nil => nil     case head :: nil=> head :: nil     case head :: tail => head :: flat(tail)   }   xss match {     case nil => nil     case head :: nil => flat(head)     case head :: tail => flat(head) :: flatten(tail)   } }  flatten(a) 

you can pattern match deep structure:

def flatten[t](xss: list[list[t]]): list[t] = xss match {    case nil => nil    case nil :: tail => flatten(tail)    case (innerhead :: innertail) :: tail => innerhead :: flatten(innertail :: tail) } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -