duplicates - Homework: Lisp items that appear more than once in a list -


Looking at a list, I am trying to return a new one whose only one is the first The list that appears more than once in the bar, I get the list as a parameter.

I did the following:

  (defun myf (lista) (if (endp lista) zero (if (member (first list) (rest list) (app) (Myf (rest lista)))))  

If I run the following: (myf '(AABABC) )) , it returns (AAB) . How can I return items only once (i.e., double A is not)?

Vincent, Gisu and Jerry indicated that you need to know that The result is already included in the list, while Derek indicated that you can modify the original list when you see that an item has been repeated. Read the documentation of the

adjoin and functions

This is very useful reference, add it to your bookmarks.

Those two functions do not modify their logic, but as a result, they return as a new list. There are other functions that modify their reasoning and thus can be more efficient, but at this time you probably should not have to worry about them.

Okay, I hope you have figured out as long as I wrote to you. If not, then keep trying, this is the only way you will actually learn.

Now I would like to talk to you about any other approach, and this is about taking the result through a recurring call.

Our function repeated will not do anything, but will call a support function repeating , which will do the actual work, it returns the initial empty result '() :

  (repeated (lst) (repeatedly lst' ()))  

now define auxiliary work , It receives two parameters: a searchable list for duplicates, and the result list where we will store duplicate items.

  (defun reper (lst result) (if (null list) result (if (member (twelfth (first of all) result)))  

When the status (member (first list) (left list)) keeps correct we code before code <, and the result of it recursive The call will be passed as the second parameter; otherwise we just pass the result as a recursive call.

When the list is finally empty (if ( Null lst) We will return Result .

  & gt; (Frequently) (ABAbabak)) (BA)  

Note that the result is (ba) and you were expecting it to be (AB) . The execution of recurring calls and each column with pen and paper But try to follow the value of the parameter will be a good practice, and you have to play with adjoin to understand its behavior. You can modify the function like this:

  (defun reper (lst result) (if (null lst) (reverse result) (if (member (first lst) ( (Reper   

This reverse

>

Now, what about alcohol? Before moving forward, how to remove duplicate elements from the list? We can write your work like this:

  (defun reper (lst result) (if (null lst) result (if (member (first lst) (rest lst)) (reper ( Remove (first list) lst) (opposition (first lst) result)) (repeat result of the rest))))  

REPL: < / P>

  & gt; ('A' (ABCDFBD)) (BCDEFBD) Removal  

Note that we are no longer on the synonyms , instead we just have a new "Bracket cell" with (opposition (first lst result)) . cons and adjoin do the same thing, except that the value will only be added if it is not already in the list.

Hope it gives you something to play with.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -