Reputation: 697
I have this code:
(define (countHearts cards)
(let* ([count 0])
(map (λ(x)
(match x
[(hearts number) (add1 count)]
[_ #f])) cards))
count)
Where cards is a list of elements in this format "(hearts number)" And when I use:
(countHearts '( (hearts 1) (hearts 2)))
It returns (the answer should be 2):
#<procedure:count>
All I want is that my code counts the number of "(heart number)" (a type previously defined) that I have in a list, but I keep getting that answer. Does anybody have an idea? I have tried other options besides add1 like
!set count (+ count 1)
But the result is the same.
Upvotes: 0
Views: 152
Reputation: 18917
why your code doesn't work
(add1 count)
adds 1 to count but throws the result away; you need to set!
count to the result of (add1 count)
count
got out of scope and you returned the build-in procedure count
- count needs to be returned inside the let*
or let
expression (I use let
since that's sufficient here)match
expression to (list 'hearts _)
So the code becomes:
(define (countHearts cards)
(let ([count 0])
(map (λ(x)
(match x
[(list 'hearts _) (set! count (add1 count))]
[_ #f]))
cards)
count))
then
> (countHearts '((hearts 1) (hearts 2)))
2
alternative
You could use the build-in count
procedure:
(define (countHearts cards)
(count (lambda (e) (eq? (car e) 'hearts))
cards))
Upvotes: 1
Reputation: 31147
Try:
(define (count-hearts cards)
(for/sum ([card cards])
(match card
[(list 'hearts n) 1]
[_ 0])))
Upvotes: 2