<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Jun 6, 2012, at 6:17 AM, Ronald Reynolds wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; position: static; z-index: auto; "><div><span>Is it correct to say that when I call a function inside of it's own definition I am just making it repeat loop? &nbsp;</span></div><div><table cellspacing="0" style="font-family: serif; "><tbody><tr><td style="padding-left: 0px; padding-right: 0px; "><table cellspacing="0" class="RktBlk" style="white-space: inherit; text-align: left; position: static; z-index: auto; "><tbody><tr style="white-space: inherit; "><td style="padding-left: 0px; padding-right: 0px; vertical-align: baseline; white-space: inherit; "><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))" class="RktStxLink" pltdoc="x" style="text-decoration: none; color: black; ">define</a></span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">my-map</span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">f</span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">lst</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span></td></tr><tr style="white-space: inherit; "><td style="padding-left: 0px; padding-right: 0px; vertical-align: baseline; white-space: inherit; "><span class="hspace" style="font-family: monospace; ">&nbsp;&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/if.html#(form._((lib._racket/private/letstx-scheme..rkt)._cond))" class="RktStxLink" pltdoc="x" style="text-decoration: none; color: black; ">cond</a></span></td></tr><tr style="white-space: inherit; "><td style="padding-left: 0px; padding-right: 0px; vertical-align: baseline; white-space: inherit; "><span class="hspace" style="font-family: monospace; ">&nbsp;&nbsp;&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">[</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family:
 monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._empty~3f))" class="RktValLink" pltdoc="x" style="text-decoration: none; ">empty?</a></span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">lst</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._empty))" class="RktValLink" pltdoc="x" style="text-decoration: none; ">empty</a></span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60,
 36); ">]</span></td></tr><tr style="white-space: inherit; "><td style="padding-left: 0px; padding-right: 0px; vertical-align: baseline; white-space: inherit; "><span class="hspace" style="font-family: monospace; ">&nbsp;&nbsp;&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">[</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/if.html#(form._((lib._racket/private/letstx-scheme..rkt)._else))" class="RktStxLink" pltdoc="x" style="text-decoration: none; color: black; ">else</a></span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._cons))" class="RktValLink" pltdoc="x" style="text-decoration: none; ">cons</a></span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">f</span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._first))" class="RktValLink" pltdoc="x" style="text-decoration: none; ">first</a></span><span class="hspace" style="font-family: monospace;
 ">&nbsp;</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">lst</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span></td></tr><tr style="white-space: inherit; "><td style="padding-left: 0px; padding-right: 0px; vertical-align: baseline; white-space: inherit; "><span class="hspace" style="font-family: monospace; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">my-map</span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktSym" style="font-family:
 monospace; white-space: inherit; color: rgb(38, 38, 128); ">f</span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">(</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); "><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._rest))" class="RktValLink" pltdoc="x" style="text-decoration: none; ">rest</a></span><span class="hspace" style="font-family: monospace; ">&nbsp;</span><span class="RktSym" style="font-family: monospace; white-space: inherit; color: rgb(38, 38, 128); ">lst</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span><span class="RktPn" style="font-family: monospace;
 white-space: inherit; color: rgb(132, 60, 36); ">)</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">]</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span><span class="RktPn" style="font-family: monospace; white-space: inherit; color: rgb(132, 60, 36); ">)</span></td></tr></tbody></table></td></tr><tr><td style="padding-left: 0px; padding-right: 0px; "><div>&nbsp;Is this code telling racket to repeat until lst is empty? &nbsp;Thx to all for your help.&nbsp;</div></td></tr></tbody></table></div></div></div></blockquote></div><br><div>In some cases, the effect is indeed similar to a repeat loop. &nbsp;But I find function calling to be simpler and easier to understand than loops, so I would rather think of it the other way around: when you write a repeat loop, you are really doing a particularly simple form of recursion. &nbsp;Any loop can be straightforwardly rewritten as a recursion, but many if not most examples of recursion CANNOT be straightforwardly rewritten as loops.</div><div><br></div><div>In other words, if you try to interpret every recursion as a loop, a lot of the examples simply won't fit into that mold. &nbsp;You're better off thinking of recursion as calling a helper function which (by pure coincidence) happens to solve the same problem as the original function.</div><div><br></div><div>Let's re-develop "my-map" in terms of helper functions. &nbsp;It's sometimes easier to solve a restricted version of a problem than the whole problem, so let's write a version that's only guaranteed to work on very short lists.</div><div><br></div><div>; my-map-0: does the same thing as my-map, but only guaranteed to work if lst is empty.</div><div>(define (my-map-0 f lst)</div><div>&nbsp; &nbsp;(cond [(empty? lst) empty]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else (error 'my-map-0 "list too long")]))</div><div><br></div><div>Of course, that's not very useful, so let's write a version that works on SLIGHTLY longer lists.</div><div><br></div><div>; my-map-1: does the same thing as my-map, but only guaranteed to work if lst is at most one element.</div><div>(define (my-map-1 f lst)</div><div>&nbsp; &nbsp;(cond [(empty? lst) empty]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else (cons (f (first lst)) (my-map-0 f (rest lst)))]))</div><div>; note that if lst is at most one element, then (rest lst) will be empty, so my-map-0 will work</div><div><br></div><div>; my-map-2: does th same thing as my-map, but only guaranteed to work if lst is at most two elements.</div><div>(define (my-map-2 f lst)</div><div>&nbsp; &nbsp;(cond [(empty? lst) empty]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else (cons (f (first lst)) (my-map-1 f (rest lst)))]))</div><div>; note that if lst is at most two elements, then (rest lst) will be at most length 1, so my-map-1 will work</div><div><br></div><div><div>; my-map-3: does th same thing as my-map, but only guaranteed to work if lst is at most three elements.</div><div>(define (my-map-3 f lst)</div><div>&nbsp; &nbsp;(cond [(empty? lst) empty]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else (cons (f (first lst)) (my-map-2 f (rest lst)))]))</div><div>; note that if lst is at most three elements, then (rest lst) will be at most length 2, so my-map-2 will work</div><div><br></div></div><div>And so on. &nbsp;No recursion, just a bunch of helper functions, each of which solves "the same" problem, but is guaranteed to work on a different subset of cases.</div><div><br></div><div>Obviously, my-map-1, my-map-2, and my-map-3 all look extremely similar, so it would make sense to parameterize:</div><div><br></div><div>; my-map-n : does the same thing as my-map, but only guaranteed to work if lst is at most n elements.</div><div>(define (my-map-n n f lst)</div><div>&nbsp; &nbsp;(cond [(empty? lst) empty]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else (cons (f (first lst)) (my-map-n (- n 1) f (rest lst)))]))</div><div><br></div><div>Then we notice that we're not actually USING n anywhere, so we might as well leave it out:</div><div><br></div><div>(define (my-map f lst)</div><div>&nbsp; &nbsp;(cond [(empty? lst) empty]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [else (cons (f (first lst)) (my-map f (rest lst)))]))</div><br><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Stephen Bloch</div><div><a href="mailto:sbloch@adelphi.edu">sbloch@adelphi.edu</a></div></div></span></span>
</div>
<br></body></html>