[racket] Forcing termination of a for loop

From: Daniel Prager (daniel.a.prager at gmail.com)
Date: Sat Nov 9 14:53:37 EST 2013

>
> (I noticed that the code ran under Racket v5.3.4. but under
> Racket v5.2.1. none of the code ran - it showed
> for: bad sequence binding clause at: #:break?)


The #:break clause isn't available in the for form of v5.2.1:

http://download.racket-lang.org/docs/5.2.1/html/reference/for.html?q=for#(form._((lib._racket/private/base..rkt)._for))

When first learning Racket coming from an imperative programming background
it's a good idea to avoid set! for a while to get a feel for the functional
aspects of the idiom.

Here's a rewrite of the first version of your program with the set!s
removed and using straight recursion
instead of iteration:

#lang racket

(define (divides? a b)
  (= (remainder a b) 0))

(define (L i maxp number)
  (cond [(= number 1) maxp]
        [(divides? number i) (L (+ i 2) i (/ number i))]
        [else (L (+ i 2) maxp number)]))

(L 3 0 165)

[This also works as #lang scheme]

You might want to try a Racket-based text to get immersed in the functional
/ recursive idiom.
HTDP is the usual blanket recommendation (free online), but now there are
other options like Realm of Racket. Scheme-based books also have a lot to
offer.

rosettacode.org has plenty of short Racket and Scheme examples to peruse
and compare with examples in familiar languages.

Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131109/b014fe09/attachment.html>

Posted on the users mailing list.