counting if equal
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.
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.
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
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?
-
- Posts: 59
- Joined: Sun May 29, 2011 8:52 pm
- Location: NY
- Contact:
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.
Using loop will be even easier.
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)
))
(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
(defun count3(x l)
(cond
((null l) 0)
((count x (car l)) (count3 (cdr l)))
(t f)
)
)
(cond
((null l) 0)
((count x (car l)) (count3 (cdr l)))
(t f)
)
)
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)))
))
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: counting if equal
Your code is wrong for at least three reasons:
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:
- counting is not called after you increase the variable y;
- You are creating a global variable y;
- you are creating a global variable in the wrong way.
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:
- Put the variable y as an argument to the function counting;
- 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.
-
- Posts: 59
- Joined: Sun May 29, 2011 8:52 pm
- Location: NY
- Contact:
Re: counting if equal
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.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.
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 .......
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: counting if equal
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.I X Code X 1 wrote:[...]Here's the starting code, you should be able to fill in the rest with what I have provided you. It's almost done.Code: Select all
(defun my-count (x list) (length (loop .......
-
- Posts: 59
- Joined: Sun May 29, 2011 8:52 pm
- Location: NY
- Contact:
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 noticed he was supposed to use 'do' right after I posted, decided to keep it up anyway.