murali wrote: example:
> (count ‘a `(a b c a c a) ).
Returns: 3
> (count ‘d ‘(a b c a c a) )
Returns: 0
Write the LISP function “count” of exercise 1, but using iteration (do loops) this time.
For the loop part you can write this function in one line of code. You just need to figure out how to group the loop macros together.
Here's a couple things you might want to have in your function:
- length (This could be used to find the length of a generated list with only the correct elements in it -- Returning the answer)
- loop (This a given; you'll want to loop through the given list. To loop through each element of a list you can use 'in'. Next, you'll probably want a condition: You can use 'if' right inside the loop macro. Simply say, if the given parameter (the first one) is equal to the current element of the list, 'collect' it into a list. This will be done for each element of the list and then length will return the final answer.
- Code: Select all
(defun my-count (x list)
(length (loop .......
Here's the starting code, you should be able to fill in the rest with what I have provided you. It's almost done.