Skip to content

Instantly share code, notes, and snippets.

@yue82
Last active May 23, 2017 01:59
Show Gist options
  • Select an option

  • Save yue82/9739d1999f428d6144ae9be6d9ce6079 to your computer and use it in GitHub Desktop.

Select an option

Save yue82/9739d1999f428d6144ae9be6d9ce6079 to your computer and use it in GitHub Desktop.
Scala training S-99 P6
// http://aperiodic.net/phil/scala/s-99/
// P06: Find out whether a list is a palindrome.
// Example:
// scala> isPalindrome(List(1, 2, 3, 2, 1))
// res0: Boolean = true
import scala.annotation.tailrec
object P06 {
@tailrec
def isPalindrome[T](l: List[T]): Boolean = l match {
case Nil => true
case x :: xs => xs match {
case Nil => true
case _ => if (x == xs.last) isPalindrome(xs.init) else false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment