Last active
May 23, 2017 01:59
-
-
Save yue82/9739d1999f428d6144ae9be6d9ce6079 to your computer and use it in GitHub Desktop.
Scala training S-99 P6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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