All pastes #306346 Raw Edit

arturh

public text v1 · immutable
#306346 ·published 2007-01-05 10:19 UTC
rendered paste body
Suppose we have n steps left to climb; initially n=1. If n=0 then we are done. Otherwise, we try to call step: if it succeeds then we now have n-1 steps to go; if it fails we have n+1 steps to go. You can implement this with an explicit counter.

  (define (step-up)
    (let loop ((n 1))
      (cond ((= n 0) #f)
            ((step)  (loop (- n 1)))
            (else    (loop (+ n 1))))))

You can also implement this without an explicit counter:

  (define (step-up)
    (unless (step)
      (step-up)
      (step-up)))

The justification is straightforward: if we fall down one step as a result of failure we then have to climb the step we just fell down in addition to the one we originally intended to climb.