[racket-dev] Please help me to fix the code
I finished it. The purpose of the function is to count the occurrance of a
letter in a list. Ex:
(count-matches 'x '()) should be 0
(count-matches 'x '(a b x)) should be 1
(count-matches 'x '(x b x)) should be 2
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.
It seems that under cond, one condition can only take one expression. What
can I do if I want to do two statements?
Here is the code.
(define (count-matches s l)
(define (addone s l x)
(cond
[(empty? l) x]
[(equal? s (first l)) (addone s (rest l) (+ 1 x))]
[else (addone s (rest l) x)]))
(addone s l 0))
On Tue, May 31, 2011 at 12:31 PM, Yingjian Ma <yingjian.ma1955 at gmail.com>wrote:
> Thank you for letting me know. I'll use the user list instead of this
> board in the future.
>
>
> On Tue, May 31, 2011 at 10:59 AM, Stephen Bloch <sbloch at adelphi.edu>wrote:
>
>>
>> On May 31, 2011, at 12:01 PM, Yingjian Ma wrote:
>>
>> > I am new to Racket. I did not expect that output is
>> "#<procedure:...uments...". It is like an error message to me.
>>
>> Of course you didn't expect it; that's the way bugs happen. Marijn's
>> point is that the "#<procedure: ..." is actually the RETURNED VALUE from
>> your function, not an error message. The real question is, why is your
>> function returning a function rather than a number? (It's the "lambda".)
>>
>> Have you written a collection of test cases for this function? Have you
>> listed, inside the body of the function, an "inventory" of available
>> expressions you're likely to need? You're correctly testing whether the
>> list is empty, and if not, you're correctly testing whether the first thing
>> in the list matches what you're looking for, but if it is, you're returning
>> a function rather than a number as I think you intended.
>>
>> And as somebody pointed out, this sort of question belongs on the racket
>> or plt-edu list, not on the racket-dev list (which is for people developing
>> and maintaining Racket itself, not people learning to program in it).
>>
>>
>> Stephen Bloch
>> sbloch at adelphi.edu
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/dev/archive/attachments/20110531/886a17d0/attachment.html>