;; wordfreq.hs — word-frequency histogram using Data.Map.
;; Source: Rosetta Code "Word frequency" (Haskell entry, simplified).
;;
;; Exercises Phase 11: `import qualified Data.Map as Map`, `Map.empty`,
;; `Map.insertWith`, `Map.lookup`, `Map.findWithDefault`, `foldl`.

(define
  hk-wordfreq-source
  "import qualified Data.Map as Map\n\ncountWord m w = Map.insertWith (+) w 1 m\n\nwordFreq xs = foldl countWord Map.empty xs\n\nresult = wordFreq [\"the\", \"cat\", \"the\", \"dog\", \"the\", \"cat\"]\n")

(hk-test
  "wordfreq.hs — \"the\" counted 3 times"
  (hk-deep-force
    (hk-run (str hk-wordfreq-source "main = Map.lookup \"the\" result\n")))
  (list "Just" 3))

(hk-test
  "wordfreq.hs — \"cat\" counted 2 times"
  (hk-deep-force
    (hk-run (str hk-wordfreq-source "main = Map.lookup \"cat\" result\n")))
  (list "Just" 2))

(hk-test
  "wordfreq.hs — \"dog\" counted 1 time"
  (hk-deep-force
    (hk-run (str hk-wordfreq-source "main = Map.lookup \"dog\" result\n")))
  (list "Just" 1))

(hk-test
  "wordfreq.hs — \"missing\" not present"
  (hk-deep-force
    (hk-run (str hk-wordfreq-source "main = Map.lookup \"missing\" result\n")))
  (list "Nothing"))

(hk-test
  "wordfreq.hs — Map.size = 3 unique words"
  (hk-deep-force (hk-run (str hk-wordfreq-source "main = Map.size result\n")))
  3)

(hk-test
  "wordfreq.hs — findWithDefault for missing returns 0"
  (hk-deep-force
    (hk-run
      (str hk-wordfreq-source "main = Map.findWithDefault 0 \"absent\" result\n")))
  0)

(hk-test
  "wordfreq.hs — findWithDefault for present returns count"
  (hk-deep-force
    (hk-run
      (str hk-wordfreq-source "main = Map.findWithDefault 0 \"the\" result\n")))
  3)

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