How can I return default at loop end in Scheme?

Posted by Kufi Annan on Stack Overflow See other posts from Stack Overflow or by Kufi Annan
Published on 2013-11-08T03:50:34Z Indexed on 2013/11/08 3:53 UTC
Read the original article Hit count: 319

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).

© Stack Overflow or respective owner

Related posts about loops

Related posts about Scheme