;; nqueens.hs — n-queens solver via list comprehension + where.
;;
;; Also exercises:
;;   - multi-clause let/where binding (go 0 = ...; go k = ...)
;;   - list comprehensions (desugared to concatMap)
;;   - abs (from Prelude)
;;   - [1..n] finite range
;;
;; n=8 is too slow for a 60s timeout; n=4 and n=5 run in ~17s combined.

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

(define
  hk-nq-base
  "queens n = go n
  where
    go 0 = [[]]
    go k = [q:qs | qs <- go (k - 1), q <- [1..n], safe q qs]
safe q qs = check q qs 1
check q [] _ = True
check q (c:cs) d = q /= c && abs (q - c) /= d && check q cs (d + 1)
")

(hk-test
  "nqueens: queens 4 has 2 solutions"
  (hk-prog-val (str hk-nq-base "result = length (queens 4)\n") "result")
  2)

(hk-test
  "nqueens: queens 5 has 10 solutions"
  (hk-prog-val (str hk-nq-base "result = length (queens 5)\n") "result")
  10)

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