Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. object P06 {
  2. def isPalindrome[A](xs: List[A]): Boolean = {
  3. def matches(n: Int, xs: List[A], ys: List[A]): Boolean = (n, xs, ys) match {
  4. case (0, _, _) => true
  5. case (n, x :: xs, y :: ys) if (x == y) => matches(n - 1, xs, ys)
  6. case _ => false
  7. }
  8. // Reverse the original list.
  9. val (n, ys) = ((0, List[A]()) /: xs) { case ((n, ys), x) => (n + 1, x :: ys) }
  10. // Only need to test for equality on first half of both lists.
  11. matches((n + 1) / 2, xs, ys)
  12. }
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement