Files
rose-ash/lib/haskell/tests/program-mapgraph.sx
giles e6d6273265
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
haskell: Phase 11 conformance — wordfreq.hs (7/7) + mapgraph.hs (6/6), Phase 11 complete
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-07 11:36:19 +00:00

47 lines
1.6 KiB
Plaintext

;; mapgraph.hs — adjacency-list using Data.Map (BFS-style traversal).
;;
;; Exercises Phase 11: `import qualified Data.Map as Map`, `Map.empty`,
;; `Map.insert`, `Map.lookup`, `Map.findWithDefault`. Adjacency lists are
;; stored as `Map Int [Int]`; `neighbors` does a default-empty lookup.
(define
hk-mapgraph-source
"import qualified Data.Map as Map\n\nemptyG = Map.empty\n\naddEdge u v g = Map.insertWith add u [v] g\n where add new old = new ++ old\n\nbuild = addEdge 1 2 (addEdge 1 3 (addEdge 2 4 (addEdge 3 4 (addEdge 4 5 emptyG))))\n\nneighbors n g = Map.findWithDefault [] n g\n")
(hk-test
"mapgraph.hs — neighbors of 1"
(hk-deep-force
(hk-run (str hk-mapgraph-source "main = neighbors 1 build\n")))
(list ":" 2 (list ":" 3 (list "[]"))))
(hk-test
"mapgraph.hs — neighbors of 4"
(hk-deep-force
(hk-run (str hk-mapgraph-source "main = neighbors 4 build\n")))
(list ":" 5 (list "[]")))
(hk-test
"mapgraph.hs — neighbors of 5 (leaf, no entry) defaults to []"
(hk-deep-force
(hk-run (str hk-mapgraph-source "main = neighbors 5 build\n")))
(list "[]"))
(hk-test
"mapgraph.hs — neighbors of 99 (absent) defaults to []"
(hk-deep-force
(hk-run (str hk-mapgraph-source "main = neighbors 99 build\n")))
(list "[]"))
(hk-test
"mapgraph.hs — Map.member 1"
(hk-deep-force
(hk-run (str hk-mapgraph-source "main = Map.member 1 build\n")))
(list "True"))
(hk-test
"mapgraph.hs — Map.size = 4 source nodes"
(hk-deep-force (hk-run (str hk-mapgraph-source "main = Map.size build\n")))
4)
{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}