我正在使用 Racket 的 minikanren 库,但想使用“disj”和“conj”运算符。为了清晰起见,我希望能够更明确地声明我是使用 disj 还是 conj,而不是必须通过 conde 表达式进行解析,尤其是当表达式变得更加复杂时。我从“The Reasoned Schemer”复制了源代码:
(define (append∞ s∞ t∞)
(cond
((null? s∞) t∞)
((pair? s∞)
(cons (car s∞)
(append∞ (cdr s∞) t∞)))
(else (λ ()
(append∞ t∞ (s∞))))))
(define (disj2 g1 g2)
(λ (s)
(append∞ (g1 s) (g2 s))))
(define-syntax disj
(syntax-rules ()
[(disj) '()]
[(disj g) g]
[(disj g0 g ...) (disj2 g0 (disj g ...))]))
这适用于前两种情况
> (run* (x) (disj (== 'foo x)))
'(foo)
但仅在使用多个目标时返回第一个结果:
> (run* (x) (disj (== 'foo x) (== 'bar x) (== 'foobar x)))
'(foo)
为什么是这样?