;; palindrome.hs — palindrome check via reverse comparison.

(define
  hk-as-list
  (fn
    (xs)
    (cond
      ((and (list? xs) (= (first xs) "[]")) (list))
      ((and (list? xs) (= (first xs) ":"))
        (cons (nth xs 1) (hk-as-list (nth xs 2))))
      (:else xs))))

(define
  hk-prog-val
  (fn
    (src name)
    (hk-deep-force (get (hk-eval-program (hk-core src)) name))))

(define hk-pal-src "isPalindrome xs = xs == reverse xs\n")

(hk-test
  "isPalindrome empty"
  (hk-prog-val (str hk-pal-src "r = isPalindrome []\n") "r")
  (list "True"))

(hk-test
  "isPalindrome single"
  (hk-prog-val (str hk-pal-src "r = isPalindrome [1]\n") "r")
  (list "True"))

(hk-test
  "isPalindrome [1,2,1] True"
  (hk-prog-val (str hk-pal-src "r = isPalindrome [1,2,1]\n") "r")
  (list "True"))

(hk-test
  "isPalindrome [1,2,3] False"
  (hk-prog-val (str hk-pal-src "r = isPalindrome [1,2,3]\n") "r")
  (list "False"))

(hk-test
  "isPalindrome [1,2,2,1] True"
  (hk-prog-val (str hk-pal-src "r = isPalindrome [1,2,2,1]\n") "r")
  (list "True"))

(hk-test
  "isPalindrome [1,2,3,4] False"
  (hk-prog-val (str hk-pal-src "r = isPalindrome [1,2,3,4]\n") "r")
  (list "False"))

(hk-test
  "isPalindrome five odd True"
  (hk-prog-val (str hk-pal-src "r = isPalindrome [1,2,3,2,1]\n") "r")
  (list "True"))

(hk-test
  "isPalindrome racecar True"
  (hk-prog-val (str hk-pal-src "r = isPalindrome \"racecar\"\n") "r")
  (list "True"))

(hk-test
  "isPalindrome hello False"
  (hk-prog-val (str hk-pal-src "r = isPalindrome \"hello\"\n") "r")
  (list "False"))

(hk-test
  "isPalindrome a True"
  (hk-prog-val (str hk-pal-src "r = isPalindrome \"a\"\n") "r")
  (list "True"))

(hk-test
  "isPalindrome madam True"
  (hk-prog-val (str hk-pal-src "r = isPalindrome \"madam\"\n") "r")
  (list "True"))

(hk-test
  "not-palindrome via map"
  (hk-as-list
    (hk-prog-val
      (str hk-pal-src "r = filter isPalindrome [[1],[1,2],[1,2,1],[2,3]]\n")
      "r"))
  (list
    (list ":" 1 (list "[]"))
    (list ":" 1 (list ":" 2 (list ":" 1 (list "[]"))))))

{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
