Write a function that will count the number of elements in a list that have a specific value. The first argument is the value to look for, the second is the list |
Solution |
(define (count-elem e l)
(if (null? l)
0 ; base case, there are 0 e's in an empty list
(if (equal? e (car l))
(+ 1 (count-elem e (cdr l)))
(count-elem e (cdr l)))))
;; Better version ?
(define (count-elem e l)
(cond
((null? l) 0) ; base case, there are 0 e's in an empty list
((equal? e (car l))
(+ 1 (count-elem e (cdr l))))
(else (count-elem e (cdr l)))))
|