Skip to content

Instantly share code, notes, and snippets.

@jaturken
Created September 27, 2012 21:00
Show Gist options
  • Save jaturken/3796439 to your computer and use it in GitHub Desktop.
Save jaturken/3796439 to your computer and use it in GitHub Desktop.
Max in Scala
def max(xs: List[Int]): Int = {
def maxOfTwo(a:Int, b:Int) = if(a>b) a else b
def maxOfHeadAndTail(i: Int, l: List[Int]): Int = if (l.isEmpty) i else maxOfHeadAndTail(maxOfTwo(i, l.head), l.tail)
maxOfHeadAndTail(xs.head, xs.tail)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment