Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. package com.learner.s99
  2.  
  3. /**
  4. * (*) Find the last but one element of a list.
  5. * Example:
  6. * scala> penultimate(List(1, 1, 2, 3, 5, 8))
  7. * res0: Int = 5
  8. */
  9. object P02 {
  10. def penultimate(input: List[Any]): Any = penultimate(input, None, None)
  11.  
  12. private def penultimate(input: List[Any], secondLast: Any, last: Any): Any = input match {
  13. case List() => secondLast
  14. case head :: tail => penultimate(tail, last, head)
  15. }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement