Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.learner.s99
- /**
- * (*) Find the last but one element of a list.
- * Example:
- * scala> penultimate(List(1, 1, 2, 3, 5, 8))
- * res0: Int = 5
- */
- object P02 {
- def penultimate(input: List[Any]): Any = penultimate(input, None, None)
- private def penultimate(input: List[Any], secondLast: Any, last: Any): Any = input match {
- case List() => secondLast
- case head :: tail => penultimate(tail, last, head)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement