Hi Experts,
I am very new to programming language.
I want to achieve an O/P pattern like
A
A B
ABC
ABCD
…
Can anyone tell me how to achieve this logic.
Regards,
Kumar
Hi Experts,
I am very new to programming language.
I want to achieve an O/P pattern like
A
A B
ABC
ABCD
…
Can anyone tell me how to achieve this logic.
Regards,
Kumar
('A' to 'Z').inits.toList.map(_.mkString).reverse.foreach(println)
Thnaks for your reply martijnhoekstra.
can we do it by using like nested for loop?
If you don’t want to use the built-in inits, you could do it by manually writing out the recursion.
def rec(printed: List[Char], notYetPrinted: List[Char]): Unit = {
notYetPrinted match {
case Nil => () //done
case head :: tail => {
val toPrint = head :: printed
println(toPrint.reverse.mkString)
rec(toPrint, tail)
}
}
}
rec(Nil, ('A' to 'Z').toList)
also you can try this:
('A' to 'Z').foldLeft(""){(acc, item)=> println(acc); acc+item}