## counting if equal

Discussion of Common Lisp

### counting if equal

Write a LISP function "count" of two arguments, an atom x and a list L, which returns the number of times that x appears in the list L. Use recursion.
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.
murali

Posts: 15
Joined: Fri Jul 01, 2011 10:11 am

### Re: counting if equal

This looks like homework. Can you at least post some attempt to solve the problem so we can say what is wrong about it?
gugamilare

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

### Re: counting if equal

As the above poster said, you need to show that you attempted this. To get you thinking correctly though, think of how you might write a "list-eater" to check each element in the list. Then you will call the function again until you've gone through each element of the list. The function 'cdr' can be used to move onto each next element. All you need to do is keep a count of how many are actually what you're looking for as you go along.

Using loop will be even easier.

I X Code X 1

Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY

### Re: counting if equal

Ya my work for above one is using recursion as followos

(defun count1(x l y)
(cond
((null l) 0)
((eq x (car(l))) (setq y (+ y 1)))
(t (count1( (x (cdr(l)) y)
))
murali

Posts: 15
Joined: Fri Jul 01, 2011 10:11 am

### another trail

(defun count3(x l)
(cond
((null l) 0)
((count x (car l)) (count3 (cdr l)))
(t f)
)
)
murali

Posts: 15
Joined: Fri Jul 01, 2011 10:11 am

### the third trail

for this code i dont know the error but it seems to be corect with a small error

Code: Select all
`(setq y 0)(defun counting (x l)  (cond   ((null l) 0)   ((eq x (car l) ) (setq y (+ y 1)) )   (t  (counting x (cdr l)))))`
murali

Posts: 15
Joined: Fri Jul 01, 2011 10:11 am

### Re: counting if equal

Your code is wrong for at least three reasons:

1. counting is not called after you increase the variable y;
2. You are creating a global variable y;
3. you are creating a global variable in the wrong way.

Do not create variables assigning values to them (using setq or setf). If you want a global variable, declare it on toplevel using either defvar or defparameter. If you want a local variable, declare it using let or let*.

Fixing (1) is easy.

Note that you don't need to create variables in this case, neither global nor local. To fix (2) and (3), you can do it in two ways:
1. Put the variable y as an argument to the function counting;
2. Forget about the variable y and return the value that is returned by the recursive call of counting, increasing it when x is the first element and without increasing otherwise.

Both approaches will need a little thinking. Good luck
gugamilare

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

### Re: counting if equal

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.

I X Code X 1

Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY

### Re: counting if equal

I X Code X 1 wrote:[...]
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.

Actually, it is easier then that because loop accepts a keyword named count or counting, which is perfect for solving his problem. However, the exercise seems to require him to use do and not loop.
gugamilare

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

### Re: counting if equal

Ohh yeah, that's true. Counting would definitely work in this instance.

I noticed he was supposed to use 'do' right after I posted, decided to keep it up anyway.

I X Code X 1

Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY