Skip to content

Instantly share code, notes, and snippets.

Created February 28, 2014 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/9263513 to your computer and use it in GitHub Desktop.
Save anonymous/9263513 to your computer and use it in GitHub Desktop.
This method fails on test
test("max of a few negative numbers") {
assert(max(List(-2, -3, -4)) === -2)
}
def max( list: List[Int] ): Int = {
if ( list.isEmpty ) {
throw new NoSuchElementException
} else {
def findMax( aList: List[Int] ): Int = {
if ( aList.isEmpty ) {
0
} else {
def maxOfListTail = findMax( aList.tail )
if ( aList.head >= maxOfListTail ) {
aList.head
} else {
maxOfListTail
}
}
}
findMax( list )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment