In: Computer Science
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
; Take a helper function that calculates the count of a .
; starting with count 0
(defun count_a (l)
(labels ((helper (x ans_count)
(if (equalp 'a (car x)) (incf ans_count))
(cond ((null x) ans_count)
(t (helper (cdr x) ans_count)))))
(helper l 0)))
; Test the code here
(write (count_a '(a b (a) c)))
==============================================
SCREENSHOT: