[racket] FOR loop

From: Zee Ken (udaybhasker552009 at gmail.com)
Date: Sun May 4 23:09:26 EDT 2014

Hi. I am trying to implement the for loop.

My *for loop* structure:

 *(for     <variable>     <initial>     <limit>     <increment>     <body*
*>*

*)*

(define (fvar exp) (cadr exp))
(define (fstart exp) (car (cddr exp)))
(define (flimit exp) (car (cdddr exp)))
(define (fincr exp) (cadr (cdddr exp)))
(define (fbody exp) (caddr (cdddr exp)))

(define (for exp)
  (begin
    (define (iter current limit step)
          (if (< current limit)
                (begin
                      (eval (fbody exp))
                      (iter (+ current step) limit step))
                (display "\n")))
    (iter (fstart exp) (flimit exp) (fincr exp))))

I tried executing (for '(i 1 10 4 (display 2))) and it showed:

*222*

in the REPL.

It is working well when constants are involved. How can I incorporate the
variable binding?

I tried extracting the variable, that is, (fvar exp), it returns 'i=(quote
i). How can I define i to be the initial value?


-- 
Thanking You,
Zee Ken
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140504/94f2f529/attachment.html>

Posted on the users mailing list.