From 3e749a580407f690d00193a262eb12680b2e5fdc Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 14 Jul 2019 22:29:43 -0400 Subject: [PATCH] Fix pattern matching --- src/sicp_logic/core.clj | 9 +++++---- src/sicp_logic/db/memory.clj | 2 +- src/sicp_logic/evaluator.clj | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/sicp_logic/core.clj b/src/sicp_logic/core.clj index 41ebad1..cab6dbb 100644 --- a/src/sicp_logic/core.clj +++ b/src/sicp_logic/core.clj @@ -27,10 +27,11 @@ (defmacro query [db q] "Queries the database for assertions that match the query." - (letfn [(process-frame [q frame] - (instantiate q frame (fn [v f] (contract-question-mark v))))] - `(map ~process-frame - (qeval ~db (query-syntax-process (quote ~q)) [{}])))) + `(map (fn [frame#] + (instantiate (query-syntax-process (quote ~q)) + frame# + (fn [v# f#] (contract-question-mark v#)))) + (qeval ~db (query-syntax-process (quote ~q)) [{}]))) (defn assert! [db assertion] "Adds a new assertion to the database." diff --git a/src/sicp_logic/db/memory.clj b/src/sicp_logic/db/memory.clj index e2cea7b..28f2bc0 100644 --- a/src/sicp_logic/db/memory.clj +++ b/src/sicp_logic/db/memory.clj @@ -19,7 +19,7 @@ (:index db) (fn [index] (let [index-value (or (get index (first assertion)) [])] - (conj index-value assertion))))) + (assoc index (first assertion) (conj index-value assertion)))))) (defn store! [db assertion] diff --git a/src/sicp_logic/evaluator.clj b/src/sicp_logic/evaluator.clj index 011f8e7..68be48a 100644 --- a/src/sicp_logic/evaluator.clj +++ b/src/sicp_logic/evaluator.clj @@ -58,8 +58,8 @@ assertions and rules from the `db`." (let [q-type (first q)] (cond - (= q-type 'and) (conjoin (rest q) input-frames) - (= q-type 'or) (disjoin (rest q) input-frames) - (= q-type 'not) (negate (rest q) input-frames) + (= q-type 'and) (conjoin db (rest q) input-frames) + (= q-type 'or) (disjoin db (rest q) input-frames) + (= q-type 'not) (negate db (rest q) input-frames) (= q-type 'lisp-value) (lisp-value (rest q) input-frames) :else (simple-query db q input-frames))))