Guest User

Untitled

a guest
Nov 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. ;; adapted from the Sudoku solver in `The Joy of Clojure 2nd Edition' 2014 by Michal Fogus and Chris Houser, Chater 16 Thinking Programs
  2. (require '[clojure.core.logic :as l] ;;the logic engine
  3. '[clojure.core.logic.fd :as fd]) ;;dealing with finite domains (integer numbers)
  4.  
  5. (defn latin-squares [n num-of-sols] ;; n - size of the square, num-fo-sols - number of solutions
  6. (let [tab (vec (repeatedly (* n n) l/lvar)) ;; n by n table of logic variables
  7. rows (partition n tab) ;; rows of the table
  8. cols (apply map vector rows) ;; columns of the table
  9. points (fd/interval 0 (dec n))] ;; valid entries
  10. (l/run num-of-sols [q]
  11. (l/everyg (fn [x] (fd/in x points)) tab) ;; all entries are valid
  12. (l/everyg fd/distinct rows) ;; every row has distinct entries
  13. (l/everyg fd/distinct cols) ;; same for columns
  14. (l/== q tab)))) ;; unification
Add Comment
Please, Sign In to add comment