<div>[Moving the thread to the users mailing list]</div><div><br></div><div>Yingjian,</div><div><br></div><div>Good to see you persevering and getting it done! Congratulations.</div>
<div><br></div><div>Before I reasoned about your code, just looking at its shape was enough to imagine it was not properly indented.</div><div>You can use and abuse of DrRacket for your on good.</div><div><br></div>
<div>Using the shortcut <font face="'courier new', monospace">CTRL+I</font> let's you reindent all of your code, which gives this:</div><div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">#lang racket</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">(define (count-matches s l) </font></div><div><font class="Apple-style-span" face="'courier new', monospace"> (define (addone s l x)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> (cond</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> [(empty? l) x]</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> [(equal? s (first l)) (addone s (rest l) (+ 1 x))]</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> [else (addone s (rest l) x)]))</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> (addone s l 0))</font></div></div><div><br></div><div><br></div><div>The change in indentation makes clear that you're defining an auxiliary function and the result of calling count-matches is that of calling (addone s l 0).</div>
<div><br></div><div>BTW "addone" doesn't really sound to me to describe what the function is doing -- it not always "adds one".</div><div>
<br></div><div><br></div><div>Look how a similar implementation is possible without an inner auxiliary function:</div><div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">#lang racket</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">(define (count-matches s l) </font></div><div><font class="Apple-style-span" face="'courier new', monospace"> (cond</font></div><div><font class="Apple-style-span" face="'courier new', monospace">
[(empty? l) 0]</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> [(equal? s (first l)) (add1 (count-matches s (rest l)))]</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> [else (count-matches s (rest l))]))</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">(count-matches 'x '()) ; should be 0</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">(count-matches 'x '(a b x)) ; should be 1</font></div><div><font class="Apple-style-span" face="'courier new', monospace">(count-matches 'x '(x b x)) ; should be 2</font></div>
</div><div><br></div><div><br></div><div>[]'s</div><br clear="all">Rodolfo Carvalho<br>
<br><br><div class="gmail_quote">On Wed, Jun 1, 2011 at 01:37, Yingjian Ma <span dir="ltr"><<a href="mailto:yingjian.ma1955@gmail.com" target="_blank">yingjian.ma1955@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>I finished it. The purpose of the function is to count the occurrance of a letter in a list. Ex:</div>
<div> </div>
<div>(count-matches 'x '()) should be 0</div>
<div>(count-matches 'x '(a b x)) should be 1 </div>
<div>(count-matches 'x '(x b x)) should be 2 </div>
<div> </div>
<div>The keywords I can use are limited. Thank you all for the help. Another I need to write is to remove duplicated items from the list.</div>
<div> </div>
<div>It seems that under cond, one condition can only take one expression. What can I do if I want to do two statements?</div>
<div> </div>
<div>Here is the code. </div>
<div> </div>
<div><div>(define (count-matches s l) <br></div> (define (addone s l x)<br> (cond<br> [(empty? l) x]<br> [(equal? s (first l)) (addone s (rest l) (+ 1 x))]<br> [else (addone s (rest l) x)]))<br>
(addone s l 0))</div><div><div></div><div>
<div> </div>
<div> </div></div></div></blockquote></div>