Question: Using the programming language scheme: Define both recursive and iterative versions of a procedure occurs that counts the number of times an item occurs at
Using the programming language scheme:
Define both recursive and iterative versions of a procedure occurs that counts the number of times an item occurs at the top level in a list. Call the iterative version occurs-it. Test your procedures by counting how many times an item occurs at the top level in a list.
I wrote one procedure but I'm unsure if it is iterative or recursive. Can i get some help with determining which it is and maybe a hint to write it the other way.
;; (occurs 'a '(a b a c a d) 0) => 3 ;; (occurs 'a '(b c a (b a) c a) 0) => 2 ;; (occurs 'a (b (c d)) 0) => 0 (define occurs (lambda (item ls acc) (cond ((null? ls) acc) ((pair? ls) (cond ((eq? (car ls) item) (occurs item (cdr ls) (+ acc 1))) (else (occurs item (cdr ls) acc)))))))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
