haskell: n-queens via list comprehension + where (+2 tests, 397/397)
- fix hk-eval-let: multi-clause where/let now uses hk-bind-decls! grouping (enables go 0 / go k pattern) - add concatMap/concat/abs/negate to Prelude (list comprehension support) - cache init env in hk-env0 (eval-expr-source 5x faster)
This commit is contained in:
@@ -376,22 +376,11 @@
|
||||
hk-eval-let
|
||||
(fn
|
||||
(binds body env)
|
||||
;; Reuse hk-bind-decls! so multi-clause fun bindings in where/let
|
||||
;; are grouped into multifuns, enabling patterns like:
|
||||
;; let { go 0 = [[]]; go k = [...] } in go n
|
||||
(let ((new-env (hk-dict-copy env)))
|
||||
;; Pre-seed names for fn-clauses so closures see themselves
|
||||
;; (mutual recursion across the whole binding group).
|
||||
(for-each
|
||||
(fn (b)
|
||||
(cond
|
||||
((= (first b) "fun-clause")
|
||||
(dict-set! new-env (nth b 1) nil))
|
||||
((and
|
||||
(= (first b) "bind")
|
||||
(list? (nth b 1))
|
||||
(= (first (nth b 1)) "p-var"))
|
||||
(dict-set! new-env (nth (nth b 1) 1) nil))
|
||||
(:else nil)))
|
||||
binds)
|
||||
(for-each (fn (b) (hk-eval-let-bind! b new-env)) binds)
|
||||
(hk-bind-decls! new-env binds)
|
||||
(hk-eval body new-env))))
|
||||
|
||||
(define
|
||||
@@ -561,6 +550,12 @@ zipWith _ _ [] = []
|
||||
zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys
|
||||
fibs = 0 : 1 : zipWith plus fibs (tail fibs)
|
||||
plus a b = a + b
|
||||
concat [] = []
|
||||
concat (xs:xss) = xs ++ concat xss
|
||||
concatMap f [] = []
|
||||
concatMap f (x:xs) = f x ++ concatMap f xs
|
||||
abs x = if x < 0 then 0 - x else x
|
||||
negate x = 0 - x
|
||||
")
|
||||
|
||||
(define
|
||||
@@ -786,8 +781,12 @@ plus a b = a + b
|
||||
((has-key? env "main") (get env "main"))
|
||||
(:else env)))))
|
||||
|
||||
;; Eagerly build the Prelude env once at load time; each call to
|
||||
;; hk-eval-expr-source copies it instead of re-parsing the whole Prelude.
|
||||
(define hk-env0 (hk-init-env))
|
||||
|
||||
(define
|
||||
hk-eval-expr-source
|
||||
(fn
|
||||
(src)
|
||||
(hk-deep-force (hk-eval (hk-core-expr src) (hk-init-env)))))
|
||||
(hk-deep-force (hk-eval (hk-core-expr src) (hk-dict-copy hk-env0)))))
|
||||
|
||||
Reference in New Issue
Block a user