How can I return default at loop end in Scheme?
- by Kufi Annan
I'm trying to implement back-tracking search in Scheme. So far, I have the following:
(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
)
My functions assignment-complete and select-u pass unit tests. The argument assignment is a hash-table make with (make-hash), so it should be fine.
I believe the problem I have is related to returning false at the end of the loop, if no recursive returns a non-false value (which should be a valid assignment).