在看了苏斯曼的http://www.infoq.com/presentations/We-Really-Dont-Know-How-To-Compute演讲后,我被鼓舞地给了core.logic和core.match一次机会。我唯一知道的例子就是我小时候做过的那些约束问题的解决者。这是一个例子,在国际预防犯罪中心的课程中使用,并在谈话中提到:
贝克、库珀、弗莱彻、米勒和史密斯住在一栋只有五层楼的公寓楼的不同楼层。贝克不住在顶层。库珀不住在底层。弗莱彻既不是住在顶层也不是住在底层。米勒住在比库珀更高的楼层。史密斯不是住在弗莱彻家附近的地板上,弗莱彻不是住在库珀家附近的地板上,每个人都住在哪里?
我在rosettacode网站上找到了这个:problem#PicoLisp
但不太确定如何将其转化为clojure。我希望有人能提供一个用core.logic或core.match解决这个问题的例子。
发布于 2012-07-01 08:27:14
下面是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])发布于 2012-06-29 06:11:51
我刚开始自己进入LP,我的第一个努力就是https://github.com/amalloy/doors,它是http://rooms.jmpup.com自动生成的逻辑谜题的解决者。现在,我已经定义了一些关系,这些关系对于所有的谜题都是有用的,但是我还没有做过任何工作来通过编程方式将一个特定的谜题转化为core.logic。您可以看到我手动翻译的一个难题- 网页演示和core.logic编码。也许这会给您一个如何使用core.logic的想法,尽管我自己对它还不熟悉,所以肯定会有一些不完善的地方。
编辑
在查看picolisp解决方案之后,我认为它不会直接转换为core.logic,因为据我所知,直接否定支持还不存在。
https://stackoverflow.com/questions/11256242
复制相似问题