简体   繁体   中英

Clojure, core.logic: listo

According to https://github.com/clojure/core.logic/wiki/Differences-from-The-Reasoned-Schemer core.logic supports listo.

However, the following piece of code does not compile

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

(defn ex07 []
  (run*
    [x]
    (listo `(a b ~x d))))

It complains:

Exception: java.lang.RuntimeException: Unable to resolve symbol: listo in this context, compiling:(test/chap03.clj:8)

Question: what is going on, and how do I get listo?

listo is not implemented. core.logic does not ship with all the definitions from The Reasoned Schemer.

as user1311390 pointed out, They are available in the tests.

https://github.com/clojure/core.logic/blob/master/src/test/clojure/clojure/core/logic/tests.clj#L459 Here is the part that implements listo.

(defn pairo [p]
  (fresh [a d]
    (== (lcons a d) p)))

(defn listo [l]
  (conde
    [(emptyo l) s#]
    [(pairo l)
     (fresh [d]
       (resto l d)
       (listo d))]))

Now we can get the expected behavior. Please note that for brevity I have purposely NOT included the entire missing implementation to The Reasoned Schemer. See link above.

(run 1
  [x]
  (listo `(a b ~x d))) 
;; => (_0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM