<div>I used two procedures instead of lambda, which is acceptable but not preferred. The thing is it can only count upto 1, because addone's value is reset to 0. I cannot (not allowed to) change (count-matches s l). How can I keep the value of addone function?<br>
</div>
<div> </div>
<div>(define (addone x)<br> (+ x 1))<br>(define (count-matches s l)<br> (cond<br> [(empty? l) 0] <br> [(equal? s (first l)) (addone 0)]<br> [else (count-matches s (rest l))]))<br><br></div>
<div class="gmail_quote">On Mon, May 30, 2011 at 5:41 PM, Yingjian Ma <span dir="ltr"><<a href="mailto:yingjian.ma1955@gmail.com">yingjian.ma1955@gmail.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">I noted that (+ i 1) is not an assignment. However, I cannot use let or set! in the code.
<div>
<div></div>
<div class="h5"><br><br>
<div class="gmail_quote">On Mon, May 30, 2011 at 4:59 PM, Yingjian Ma <span dir="ltr"><<a href="mailto:yingjian.ma1955@gmail.com" target="_blank">yingjian.ma1955@gmail.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>lambda is used to define a function i and i will increment by 1 when a macth is found.<br>It seems it did not work. In fact I do not want to redefine i after the first recursion. But I do not know how to fix it.</div>
<div> </div>
<div>Thank you for the quick reply.</div>
<div>
<div></div>
<div>
<div> </div>
<div class="gmail_quote">On Mon, May 30, 2011 at 4:29 PM, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div><br>On May 30, 2011, at 7:17 PM, Yingjian Ma wrote:<br><br>> Hi All,<br>><br>> I wrote a piece of code to count the occurrance of a letter in a list. But it gave me an error saying "#<procedure:...uments..."<br>
><br>> The code is below:<br>><br>> (define (count-matches s l)<br>> (cond<br>> [(empty? l) 0]<br>> [(equal? s (first l)) (lambda (i)(+ i 1))]<br><br></div>What's this lambda doing here?<br>
<div><br><br>> [else (count-matches s (rest l))]))<br>><br>> Where the s is a letter and l is a list. To use it as an example, I typed<br>><br>> (count-matches 'f '(f ))<br>><br>> and got the error. The right result should be 1.<br>
><br>> Do I need to initialize i? If so, how can I do it?<br>><br>> Note that the keywords that I can use are pretty much used in the code. If the solution needs keywords outside this post, it may not be useful to me.<br>
><br>> Thank you very much.<br></div>> _________________________________________________<br>> For list-related administrative tasks:<br>> <a href="http://lists.racket-lang.org/listinfo/dev" target="_blank">http://lists.racket-lang.org/listinfo/dev</a><br>
<br></blockquote></div><br></div></div></blockquote></div><br></div></div></blockquote></div><br>