Reputation: 9
Function countSymbols
takes a nested list of numbers and symbols as input, and returns the count of all symbols in the input list.
I had tried to get the output but couldn't do it. Can you help me with this. This is my code:
(define (countsymbols mylist)
(if (null? mylist) mylist
(if (symbol? (car mylist))
(+ (countsymbols(car mylist)) (countsymbols( cdr mylist)))
(+1 (countsymbols (cdr mylist))))))
Output should look like this:
(countSymbols '(2 56 x (1 y)))
returns 2
Upvotes: 1
Views: 853
Reputation: 18917
You were close, but had 2 errors:
Like so:
(define (countsymbols mylist)
(if (null? mylist)
0
(let ((c (car mylist)))
(cond
((list? c) (+ (countsymbols c) (countsymbols (cdr mylist))))
((symbol? c) (+ 1 (countsymbols (cdr mylist))))
(else (countsymbols (cdr mylist)))))))
Upvotes: 2