Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a simple program to reverse a List of elements without using utility method(reverse) in Scala.
Let’s look at the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.clearurdoubt object Reversing { def main(args: Array[String]) { val list = List(3,4,5,1,2,3) val rev = reversed(list) println(rev + " @@ " + list.reverse) } def reversed(input: List[Int]): List[Int] = input match { case List() => input case x :: xs => reversed(xs) ::: List(x) } } |
Output:
Happy Learning.
Please leave a reply in case of any queries.