Solving Dinesman's multiple-dwelling example using clojure's core.logic/core.match -


after watching sussman's lecture http://www.infoq.com/presentations/we-really-dont-know-how-to-compute, inspired give core.logic , core.match go. examples know constraint problem solvers used kid. 1 example used in sicp course being mentioned in talk:

baker, cooper, fletcher, miller, , smith live on different floors of apartment house contains 5 floors. baker not live on top floor. cooper not live on bottom floor. fletcher not live on either top or bottom floor. miller lives on higher floor cooper. smith not live on floor adjacent fletcher's. fletcher not live on floor adjacent cooper's. live?

i found on rosettacode site: http://rosettacode.org/wiki/dinesman%27s_multiple-dwelling_problem#picolisp

but not sure how translates clojure. hoping can provide example of solving using core.logic or core.match

here's solution in core.logic. it's not equivalent picolisp algorithm because don't have same primitives available, it's same general idea. introducing me problem - fun invent permuteo , beforeo, , had first excuse use conda. edit: using conda there horrible , wrong, , i'm conde now. oh well, day.

(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)     (!= 'cooper)     (!= 'fletcher)     (!= e 'fletcher)     (beforeo 'cooper 'miller tenants)     (not-adjacento 'smith 'fletcher tenants)     (not-adjacento 'fletcher 'cooper tenants)))  ;; ([smith cooper baker fletcher miller]) 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -