Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is not only one possible solution, but I will stay close to your code. Since this is homework, I will not give you a working answer, but I will try to give you some things to think about, and give detailed pointers:</p> <p>Try to understand what your code does and what you really want it to do:</p> <pre><code>(defun remove-all (a l) (cond ((null l) nil) ((eql (car l) a) (delete a (cdr l))) (t (cons (car l) (delete a (cdr l)))))) </code></pre> <p>(Renamed to <code>remove-all</code> because <code>delete</code> is already taken, and re-indented in a sane manner.)</p> <p>For flat lists, the code seems to work; but how about nested lists? Let's look at an easy example:</p> <ol> <li>What will happen if you evaluate <code>(remove-all 1 '((1)))</code>?</li> <li>What do you want to happen for that input?</li> <li>How can you achieve it?</li> </ol> <p>Let's take a look:</p> <ol> <li><p>What happens:</p> <ul> <li>The list is not <code>null</code>, go on</li> <li>The <code>car</code> is not <code>eq</code> to <code>1</code> go on</li> <li><code>'(1)</code> gets <code>cons</code>ed to <code>(remove-all '())</code>, yielding <code>'((1))</code></li> </ul> <p>So, it failed to recognize that the <code>car</code> is itself a list which should be searched for matching elements. The problem seems to lie between step one and step two.</p></li> <li><p>What should be done:</p> <ul> <li>Check, if the <code>car</code> is itself a list</li> <li>If yes, invoke <code>remove-all</code> on it</li> <li>Then, <code>cons</code> the result to the <code>cdr</code>, which also needs to be "cleaned" (Hint: But only <em>if</em> there is something to <code>cons</code>)</li> </ul></li> <li><p>How exactly?</p> <ul> <li>Add a <code>cond</code> clause which does the things mentioned under 2 -- Left as homework</li> </ul></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload