1

I'm doing clojure/core.logic koans and stuck on this one:

"Here we give run a specific number to control how many answers we get. Think
carefully. Is there only one list in the universe that satisfies this relation?
Are there infinitely many?"
 (= (run 1 [q]
       (membero 'cat q))
    [__])

Running (run 1 [q] (membero 'cat q)) in the REPL said me that the answer is ((cat . _.0)). I'm not quite sure what the dot in the middle means, but anyway, sticking '(cat . _.0) instead of the __ placeholder in the original koan doesn't help (assertion still fails). Could you please point me to the right direction? And also explain what the dot between cat and _.0 means? My guess is that it means what follows (i.e. _.0) is a tail of any length, but I'm not 100% sure. === UPDATE

amalloy pointed me to the right direction (thank you, sir!). lcons does the trick:

 (= (run 1 [q]
       (membero 'cat q))
    [(lcons 'cat '_.0)])

And a bit of REPL:

user=> (lcons 'cat '_.0)
(cat . _.0)
user=> '(cat . _.0)
(cat . _.0)
user=> (= '(cat . _.0) (lcons 'cat '_.0))
false

The dot is used to represent, well, dotted lists. According to Wikipedia, a dotted list is a kind of improper lists, where another kind is a circular list. So, in the REPL session above, the first list was a dotted list with two elements ('cat and '_.0), whereas the second list was a proper list with three elements ('cat', '. and '._0). Their string representation are the same, but nevertheless they are different.

4

1 回答 1

2

(a . b)符号是其他 lisp 的遗留物,这些 lisp 使用 cons 单元来构建不正确的列表。你是对的,它的意思是“符号 cat 后跟任何长度的任何列表”,但实际上它是 core.logic LCons 值的打印表示。没有简单的文字可以输入来生成该类型的值,因此您无法真正将其与您自己的值进行比较。但是,我希望 LCons 是可排序的,因此您可以与 进行比较(first the-thing)'cat依此类推。

于 2013-08-28T22:00:10.367 回答