Page 1 of 1

counting if equal

Posted: Wed Jul 06, 2011 2:18 pm
by murali
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.

Re: counting if equal

Posted: Wed Jul 06, 2011 2:41 pm
by gugamilare
This looks like homework. Can you at least post some attempt to solve the problem so we can say what is wrong about it?

Re: counting if equal

Posted: Wed Jul 06, 2011 2:55 pm
by I X Code X 1
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.

Re: counting if equal

Posted: Wed Jul 06, 2011 3:01 pm
by murali
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)
))

another trail

Posted: Wed Jul 06, 2011 3:43 pm
by murali
(defun count3(x l)
(cond
((null l) 0)
((count x (car l)) (count3 (cdr l)))
(t f)
)
)

the third trail

Posted: Wed Jul 06, 2011 5:06 pm
by murali
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)))
))

Re: counting if equal

Posted: Wed Jul 06, 2011 7:29 pm
by gugamilare
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 ;)

Re: counting if equal

Posted: Wed Jul 06, 2011 9:35 pm
by I X Code X 1
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.

Re: counting if equal

Posted: Wed Jul 06, 2011 10:39 pm
by gugamilare
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.

Re: counting if equal

Posted: Thu Jul 07, 2011 6:12 am
by I X Code X 1
Ohh yeah, that's true. Counting would definitely work in this instance. :lol:

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