Dinesman multi-tenant solution using clojure core.logic / core.match

After watching Sussman's lecture http://www.infoq.com/presentations/We-Really-Dont-Know-How-To-Compute , I am inspired to give core.logic and core.match a go. The only examples I know are the problem solvers of the constraints that I did in childhood. This was an example used in the SICP course, as well as a mention in the conversation:

Baker, Cooper, Fletcher, Miller and Smith live on different floors of a residential building, which consists of only five floors. Baker doesn't live on the top floor. Cooper doesn't live on the ground floor. Fletcher does not live on the upper or lower floor. Miller lives on a higher floor than Cooper. Smith does not live on the floor adjacent to Fletcher. Fletcher does not live on the floor adjacent to Cooper. Where does everyone live?

I found this on the rosettacode website: http://rosettacode.org/wiki/Dinesman%27s_multiple-dwelling_problem#PicoLisp

But not too sure how this translates to clojure. I hope someone can provide an example of a solution to this problem using core.logic or core.match

+5
source share
2 answers

core.logic. picolisp, , . , - permuteo beforeo, conda. : conda , conde . , -.

(ns dwelling.core
  (:refer-clojure :exclude [==])
  (:use clojure.core.logic))

(defn rembero [x l out]
  (fresh [head tail]
    (conso head tail l)
    (conde [(== x head) (== out tail)]
           [(fresh [new-out]
              (conso head new-out out)
              (rembero x tail new-out))])))

(defn permuteo [a b]
  (conde [(emptyo a) (emptyo b)]
         [(fresh [head tail b-tail]
            (conso head tail a)
            (rembero head b b-tail)
            (permuteo tail b-tail))]))

(defn beforeo [x y l]
  (fresh [head tail]
    (conso head tail l)
    (conde [(== x head) (fresh [more-tail]
                          (rembero y tail more-tail))]
           [(beforeo x y tail)])))

(defn not-adjacento [x y l]
  (fresh [head tail more]
    (conso head tail l)
    (resto tail more)
    (conde [(== x head) (membero y more)]
           [(== y head) (membero x more)]
           [(not-adjacento x y tail)])))

(run* [tenants]
  (fresh [a b c d e]
    (== [a b c d e] tenants)
    (permuteo tenants '[Cooper Baker Fletcher Miller Smith])
    (!= e 'Baker)
    (!= a 'Cooper)
    (!= a 'Fletcher)
    (!= e 'Fletcher)
    (beforeo 'Cooper 'Miller tenants)
    (not-adjacento 'Smith 'Fletcher tenants)
    (not-adjacento 'Fletcher 'Cooper tenants)))

;; ([Smith Cooper Baker Fletcher Miller])
+3

LP , https://github.com/amalloy/doors, http://rooms.jmpup.com. , , , , core.logic . , - - core.logic encoding. , , core.logic, , .

picolisp , core.logic, , , .

+2

All Articles