<div dir="ltr"><div><div><span style="font-family:courier new,monospace"><span style="font-family:arial,helvetica,sans-serif">Hi. I am trying to implement the for loop.</span><br><br></span></div><font><span dir="ltr"><font color="black" face="Calibri,Arial,Helvetica,sans-serif"><span style="background-color:white"><div style="text-align:justify;margin-top:0px;margin-bottom:0px" align="left">
My <b><u>for loop</u></b> structure:</div>
<div style="text-align:justify;margin-top:0px;margin-bottom:0px" align="left"><br>
</div>
<div style="text-align:justify;margin-top:0px;margin-bottom:0px" align="left"><b>(for <variable> <initial> <limit> <increment> <body</b><b>></b><b>)<br><br></b></div></span></font></span></font></div>
<span style="font-family:courier new,monospace"></span><div><div><div><span style="font-family:courier new,monospace"><br>(define (fvar exp) (cadr exp))<br>(define (fstart exp) (car (cddr exp)))<br>(define (flimit exp) (car (cdddr exp)))<br>
(define (fincr exp) (cadr (cdddr exp)))<br>(define (fbody exp) (caddr (cdddr exp)))<br><br>(define (for exp)<br> (begin<br> (define (iter current limit step)<br> (if (< current limit)<br> (begin<br>
(eval (fbody exp))<br> (iter (+ current step) limit step))<br> (display "\n")))<br> (iter (fstart exp) (flimit exp) (fincr exp))))</span><br><br></div>
<div>I tried executing <span style="font-family:courier new,monospace">(for '(i 1 10 4 (display 2)))</span> and it showed:<br><br><span style="font-family:courier new,monospace"><b><span style="color:rgb(53,28,117)">222</span></b></span><br>
<br></div><div>in the REPL.<br><br></div><div>It is working well when constants are involved. How can I incorporate the variable binding?<br><br></div><div>I tried extracting the variable, that is, <span style="font-family:courier new,monospace">(fvar exp)</span>, it returns <span style="font-family:courier new,monospace">'i=(quote i)</span>. How can I define <span style="font-family:courier new,monospace">i</span> to be the initial value?<br>
<br clear="all"></div><div><br>-- <br><div dir="ltr"><div>Thanking You,<br></div>Zee Ken<br></div>
</div></div></div></div>