[racket] Forcing termination of a for loop

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sun Nov 10 06:26:31 EST 2013

I agree fully with Daniel's suggestions.

Daniel's first example is also in a style popular among Racketeers.  
Since I think not everyone has exactly the same verbal/visual cognitive 
makeup, below is the same code with some slight tweaks in style, to 
appeal to different people.  (You'll want to view it with a fixed-pitch 
font, like in DrRacket if not in how you're reading this email, since 
whitespace is used for exposing symmetries in the syntax.)

(define (divides? a b)
   (zero? (remainder a b)))

(define (greatest-factor number)
   (let loop ((i      3)
              (maxp   0)
              (number number))
     (cond ((= 1      number)   maxp)
           ((divides? number i) (loop (+ 2 i) i    (/ number i)))
           (else                (loop (+ 2 i) maxp    number)))))

(greatest-factor 165)

Neil V.


Posted on the users mailing list.