[racket] Forcing termination of a for loop
On Sat, Nov 09, 2013 at 11:53:37AM -0800, Daniel Prager wrote:
> >
> > (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
Thank you so much for that Dan - I spent a week trying to do that! I
wil go away and read some of your recommendations.
Bob