a pastebin project

Untitled

  1. 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.
  2.  
  3.   (define (step-up)
  4.     (let loop ((n 1))
  5.       (cond ((= n 0) #f)
  6.             ((step)  (loop (- n 1)))
  7.             (else    (loop (+ n 1))))))
  8.  
  9. You can also implement this without an explicit counter:
  10.  
  11.   (define (step-up)
  12.     (unless (step)
  13.       (step-up)
  14.       (step-up)))
  15.  
  16. 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.

advertising

Create a Paste

Please enter your new post below (or upload a file instead):





Please note that information posted here will not expire by default. If you want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords.

worth-right
fantasy-obligation
fantasy-obligation