Im trying count the number of positive elements in a list. Here is what I have so far:
(define howMany
(lambda (list)
(cond
[(not (list? list)) 0]
[(null? list) 0]
[(> list 0) (+ 1 (howMany (cdr list)))])))
It keeps giving me an error, “expects type real number”, how would you fix this?
Oh im calling this like so:
(howMany '(6 7 8))
There are a couple of bugs in your code.
(> list 0)should be(> (car list) 0)as you want to check if the first element of the list is greater than 0. You cannot apply the default implementation of>to a list either.(+ 1 (howMany (cdr list)))will also fail ashowManydoes not always evaluate to a number. You have to maintain a counter by passing that as an argument to the recursively called procedure. One way to do this is:Test: