Scheme early "short circuit return"?

Posted by Suan on Stack Overflow See other posts from Stack Overflow or by Suan
Published on 2010-03-12T16:50:58Z Indexed on 2010/03/12 18:17 UTC
Read the original article Hit count: 498

Filed under:
|
|
|

I'm trying to find out how I can do an "early return" in a scheme procedure without using a top-level if or cond like construct.

(define (win b)
 (let* ((test (first (first b)))
        (result (every (lambda (i) (= (list-ref (list-ref b i) i) test))
                       (enumerate (length b)))))
  (when (and (not (= test 0)) result) test))
 0)

For example, in the code above, I want win to return test if the when condition is met, otherwise return 0. However, what happens is that the procedure will always return 0, regardless of the result of the when condition.

The reason I am structuring my code this way is because in this procedure I need to do numerous complex checks (multiple blocks similar to the let* in the example) and putting everything in a big cond would be very unwieldy.

© Stack Overflow or respective owner

Related posts about Scheme

Related posts about short-circuiting