;; anagram.hs — anagram detection using sort.

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

(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-ana-src
  "isAnagram xs ys = sort xs == sort ys\n\nhasAnagram needle haystack = any (isAnagram needle) haystack\n")

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

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

(hk-test
  "isAnagram [] [] True"
  (hk-prog-val (str hk-ana-src "r = isAnagram [] []\n") "r")
  (list "True"))

(hk-test
  "isAnagram [1] [1] True"
  (hk-prog-val (str hk-ana-src "r = isAnagram [1] [1]\n") "r")
  (list "True"))

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

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

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

(hk-test
  "hasAnagram [1,2] [[3,4],[2,1],[5,6]] True"
  (hk-prog-val
    (str hk-ana-src "r = hasAnagram [1,2] [[3,4],[2,1],[5,6]]\n")
    "r")
  (list "True"))

(hk-test
  "hasAnagram [1,2] [[3,4],[5,6]] False"
  (hk-prog-val (str hk-ana-src "r = hasAnagram [1,2] [[3,4],[5,6]]\n") "r")
  (list "False"))

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