Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def max(list: List[Int]): Int = {
- if(list.isEmpty) Int.MinValue
- else list.head max max(list.tail)
- }
- def max2(list: List[Int]): Int = {
- if(list.tail.isEmpty) list.head
- else {
- val newMax = max(list.tail)
- if(list.head > newMax) list.head
- else newMax
- }
- }
- =====
- Or we can use an accumulating parameter? and something involving a helper function
- def maxH(maxSoFar: Int, list: List[Int]): Int = {
- if (list.isEmpty) maxSoFar
- else maxH(maxSoFar max list.head, list.tail)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement