Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the LISP function (use car,cdr,cddr,cons,cond,atom,setq,lambda, letn lable

ID: 3877766 • Letter: W

Question

Write the LISP function (use car,cdr,cddr,cons,cond,atom,setq,lambda, letn lable)
(count-sub-list-occ L1 L2)
That returns the number of occurrences of the list L1 as a sublist of L2. L1 is a sublist of L2 if all the elements of L2 can be found in sequence in L1.
For example: (1 2) is a sublistof (3 1 2)
(1 3) is not a sublist of (1 2 3)
(1 3) is not a sublist of (3 2 1)
(1 1) is a sublist of (2 3 2 3 1 1 3 2)
(count-sub-list-occ '(1 2) '(1 2 3))1
(count-sub-list-occ '(1 2) '(3 2 3))0
(count-sub-list-occ '(1 2) '(1 2 1 2 3))2
(count-sub-list-occ '(1 1) '(1 1 1 1))3

Explanation / Answer

(defun count-sublists (lst) (cond ((null lst) 0) ((atom lst) 0) ((atom (first lst)) (count-sublists (rest lst))) (t (+ 1 (count-sublists (first lst)) (count-sublists (rest lst))))))