-
-
Save totem3/4186081 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'clojure.core.logic) | |
(defne append [x y z] | |
([() a a]) | |
([[a . b] _ [a . c]] | |
(append b y c))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use '[clojure.core.match :only [match]]) | |
(defn append [x y] | |
(match [x] | |
[[]] y | |
[[h & t]] (recur t (cons h y)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(assert (= (run* [q] (append [1 2] [3 4] q)) | |
'((1 2 3 4)))) | |
(assert (= (run* [q] (append [1 2] q [1 2 3 4])) | |
'((3 4)))) | |
(assert (= (run* [q] (fresh [a b] | |
(== q [a b]) | |
(append a b [1 2 3 4]))) | |
'([() (1 2 3 4)] [(1) (2 3 4)] [(1 2) (3 4)] [(1 2 3) (4)] [(1 2 3 4) ()]))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'clojure.core.logic) | |
(def _1 [()]) | |
(def _2 [() ()]) | |
(def _3 [() () ()]) | |
(def _4 [() () () ()]) | |
(defne different [list] | |
([()]) | |
([[x . ()]]) | |
([[x . [h . t]]] | |
(fresh [l] | |
(!= x h) | |
(conso x t l) | |
(different l)))) | |
(defne lte [m n] | |
([() _]) | |
([[_ . x] [_ . y]] | |
(lte x y))) | |
(defne gte [m n] | |
([_ ()]) | |
([[_ . x] [_ . y]] | |
(gte x y))) | |
(defne bounds [l m n] | |
([() _ _]) | |
([[h . t] m n] | |
(lte m h) | |
(gte n h) | |
(bounds t m n))) | |
(defne sudoku [puzzle] | |
([[s11 s12 s13 s14 | |
s21 s22 s23 s24 | |
s31 s32 s33 s34 | |
s41 s42 s43 s44]] | |
(bounds [s11 s12 s13 s14 | |
s21 s22 s23 s24 | |
s31 s32 s33 s34 | |
s41 s42 s43 s44] _1 _4) | |
(different [s11 s12 s13 s14]) | |
(different [s21 s22 s23 s24]) | |
(different [s31 s32 s33 s34]) | |
(different [s41 s42 s43 s44]) | |
(different [s11 s21 s31 s41]) | |
(different [s12 s22 s32 s42]) | |
(different [s13 s23 s33 s43]) | |
(different [s14 s24 s34 s44]) | |
(different [s11 s12 s21 s22]) | |
(different [s13 s14 s23 s24]) | |
(different [s31 s32 s41 s42]) | |
(different [s33 s34 s43 s44]))) | |
(run* [q] (sudoku [_3 _1 _2 _4 _4 _2 _3 _1 _1 _3 _4 _2 _2 _4 _1 _3])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment