我正在尝试在 Scheme 中实现回溯搜索。到目前为止,我有以下内容:
(define (backtrack n graph assignment)
(cond (assignment-complete n assignment) (assignment) )
(define u (select-u graph assignment))
(define c 1)
(define result 0)
(let forLoop ()
(when (valid-choice graph assignment c)
(hash-set! assignment u c)
(set! result (backtrack n graph assignment))
(cond ((not (eq? result #f)) result))
(hash-remove! assignment u)
)
(set! c (+ c 1))
(when (>= n c) (forLoop))
)
#f ; I believe this is where I'm having problems
)
我的函数 assignment-complete 和 select-u 通过了单元测试。参数赋值是一个带有 (make-hash) 的哈希表,所以应该没问题。
我相信我遇到的问题与在循环结束时返回 false 有关,如果没有递归返回非 false 值(这应该是有效的赋值)。是否有等效于显式返回语句的方案?