[racket-dev] Please help me to fix the code

From: Yingjian Ma (yingjian.ma1955 at gmail.com)
Date: Mon May 30 22:31:20 EDT 2011

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?

(define (addone x)
     (+ x 1))
(define (count-matches s l)
      (cond
     [(empty? l) 0]
     [(equal? s (first l)) (addone 0)]
     [else (count-matches s (rest l))]))

On Mon, May 30, 2011 at 5:41 PM, Yingjian Ma <yingjian.ma1955 at gmail.com>wrote:

> I noted that (+ i 1) is not an assignment.  However, I cannot use let or
> set! in the code.
>
>
> On Mon, May 30, 2011 at 4:59 PM, Yingjian Ma <yingjian.ma1955 at gmail.com>wrote:
>
>> lambda is used to define a function i and i will increment by 1 when a
>> macth is found.
>> 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.
>>
>> Thank you for the quick reply.
>>
>> On Mon, May 30, 2011 at 4:29 PM, Matthias Felleisen <matthias at ccs.neu.edu
>> > wrote:
>>
>>>
>>> On May 30, 2011, at 7:17 PM, Yingjian Ma wrote:
>>>
>>> > Hi All,
>>> >
>>> > 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..."
>>> >
>>> > The code is below:
>>> >
>>> > (define (count-matches s l)
>>> >       (cond
>>> >      [(empty? l) 0]
>>> >      [(equal? s (first l)) (lambda (i)(+ i 1))]
>>>
>>> What's this lambda doing here?
>>>
>>>
>>> >      [else (count-matches s (rest l))]))
>>> >
>>> > Where the s is a letter and l is a list.  To use it as an example, I
>>> typed
>>> >
>>> > (count-matches 'f '(f ))
>>> >
>>> > and got the error.  The right result should be 1.
>>> >
>>> > Do I need to initialize i?  If so, how can I do it?
>>> >
>>> > 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.
>>> >
>>> > Thank you very much.
>>> > _________________________________________________
>>> >  For list-related administrative tasks:
>>> >  http://lists.racket-lang.org/listinfo/dev
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/dev/archive/attachments/20110530/1416a260/attachment.html>

Posted on the dev mailing list.