In: Computer Science
You can only use built in Lisp functions and you cannot use setq function.
Write a function in Lisp called f1 that counts the number of lists in a list. Example: (f1 ‘(a (a b (b c)) c d (e))) returns 2
check out the solution.
--------------------------
CODE:
; f1 function implementation
(defun f1 (lst)
; multiple condition list
(cond ((null lst) 0) ; if list is empty then return 0
; check each list item is list if yes then count it and recursive
call
((listp (car lst)) (+ 1 (f1 (cdr lst))))
; else recursive call without counting
(t (f1 (cdr lst)))
)
)
; function call and print the result
(print (f1 '(a (a b (b c)) c d (e))))
(print (f1 '(a (a b (b c)) c d)))
(print (f1 '(a (a b) (b (c)) (c) d)))
---------------------------------------
-----------------------------------------------------------------------
OUTPUT :
=============================================