我有这段代码,它按我的意愿,递归地拉给定人的父母:
(defn anc [child]
(run* [q]
(conde
[(fresh [?p]
(parento child ?p)
(?== q [child ?p]))]
[(fresh [?p ?gp]
(parento child ?p)
(parento ?p ?gp)
(?== q [ ?p ?gp]))]
[(fresh [?p ?gp ?ggp]
(parento child ?p)
(parento ?p ?gp)
(parento ?gp ?ggp)
(?== q [ ?gp ?ggp]))]
)))
问题是,对于每一代我返回,我必须添加一个新的测试。
是否有一种方法可以在core.logic中推广这一点?
我尝试了一些方法,希望能对你有所帮助:
user=> (defrel parent c p)
user=> (fact parent :b :a)
nil
user=> (fact parent :c :b)
nil
user=> (fact parent :d :c)
nil
user=> (defn anso [c a]
(conde
[(fresh [p x]
(parent c p)
(anso p x)
(appendo [p] x a))]
[(fresh [x]
(parent c x)
(== a [x]))]))
#'user/anso
user=> (last (run* [q] (anso :c q)))
(:b :a)
user=> (last (run* [q] (anso :b q)))
[:a]
user=> (last (run* [q] (anso :d q)))
(:c :b :a)