Recursive Max in Scala

Determining the maximum value in a collection via a recursive Scala function.
def recursiveMax(xs: List[Int]): Int = 
    def compareMax(x: Int, y: Int): Int = 
        if (x > y) x
        else y
    if (xs.isEmpty) 0
    else compareMax(xs.head, recursiveMax(xs.tail))

recursiveMax(List(2, 6, 1, 6, 8, 10, 11, 0, 11, 20, 20))

// res0: Int = 20
Scala
Scala is a strong, statically typed, high-level, general-purpose programming language that supports both object-oriented programming and functional programming. It…
Programming paradigms play a crucial role in the realm of computer science. They act as blueprints or frameworks to…
Machine Learning is a field focused on developing, comprehending, and utilizing computer programs that can learn from experience to model,…
Higher-order functions take other functions as arguments and/or return other functions as a result. These powerful abstractions allow us…

Request Full Resume