;; roman.hs — convert integers to Roman numerals with guards + ++.

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

(define
  hk-rom-src
  "toRoman 0 = \"\"\ntoRoman n\n  | n >= 1000 = \"M\"  ++ toRoman (n - 1000)\n  | n >= 900  = \"CM\" ++ toRoman (n - 900)\n  | n >= 500  = \"D\"  ++ toRoman (n - 500)\n  | n >= 400  = \"CD\" ++ toRoman (n - 400)\n  | n >= 100  = \"C\"  ++ toRoman (n - 100)\n  | n >= 90   = \"XC\" ++ toRoman (n - 90)\n  | n >= 50   = \"L\"  ++ toRoman (n - 50)\n  | n >= 40   = \"XL\" ++ toRoman (n - 40)\n  | n >= 10   = \"X\"  ++ toRoman (n - 10)\n  | n >= 9    = \"IX\" ++ toRoman (n - 9)\n  | n >= 5    = \"V\"  ++ toRoman (n - 5)\n  | n >= 4    = \"IV\" ++ toRoman (n - 4)\n  | otherwise = \"I\"  ++ toRoman (n - 1)\n")

(hk-test
  "toRoman 1 = I"
  (hk-prog-val (str hk-rom-src "r = toRoman 1\n") "r")
  "I")

(hk-test
  "toRoman 4 = IV"
  (hk-prog-val (str hk-rom-src "r = toRoman 4\n") "r")
  "IV")

(hk-test
  "toRoman 5 = V"
  (hk-prog-val (str hk-rom-src "r = toRoman 5\n") "r")
  "V")

(hk-test
  "toRoman 9 = IX"
  (hk-prog-val (str hk-rom-src "r = toRoman 9\n") "r")
  "IX")

(hk-test
  "toRoman 10 = X"
  (hk-prog-val (str hk-rom-src "r = toRoman 10\n") "r")
  "X")

(hk-test
  "toRoman 14 = XIV"
  (hk-prog-val (str hk-rom-src "r = toRoman 14\n") "r")
  "XIV")

(hk-test
  "toRoman 40 = XL"
  (hk-prog-val (str hk-rom-src "r = toRoman 40\n") "r")
  "XL")

(hk-test
  "toRoman 50 = L"
  (hk-prog-val (str hk-rom-src "r = toRoman 50\n") "r")
  "L")

(hk-test
  "toRoman 90 = XC"
  (hk-prog-val (str hk-rom-src "r = toRoman 90\n") "r")
  "XC")

(hk-test
  "toRoman 100 = C"
  (hk-prog-val (str hk-rom-src "r = toRoman 100\n") "r")
  "C")

(hk-test
  "toRoman 400 = CD"
  (hk-prog-val (str hk-rom-src "r = toRoman 400\n") "r")
  "CD")

(hk-test
  "toRoman 1000 = M"
  (hk-prog-val (str hk-rom-src "r = toRoman 1000\n") "r")
  "M")

(hk-test
  "toRoman 1994 = MCMXCIV"
  (hk-prog-val (str hk-rom-src "r = toRoman 1994\n") "r")
  "MCMXCIV")

(hk-test
  "toRoman 58 = LVIII"
  (hk-prog-val (str hk-rom-src "r = toRoman 58\n") "r")
  "LVIII")

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