Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first version in your code doesn't make much sense. Why use <code>lambda</code>s in this way? you're supposed to recursively call <em>the same</em> procedure being defined, not creating a one-shot anonymous procedure, that won't work for solving the problem at hand. And this part: <code>(list1)</code> is causing the error <code>The object is not applicable</code>: you're trying to invoke the list as if it were a procedure - because it's surrounded by parenthesis. Remember that in Scheme, a syntax such as this one: <code>(foo)</code> indicates that the <code>foo</code> <em>procedure</em> is to be called.</p> <p>The second version of your code is fine, that's the simple way to implement a <code>remove-all</code> procedure. A bit of nitpicking, though: when you find yourself nesting <code>if</code>s, it's a sure sign that a <code>cond</code> would be more appropriate. Also notice that it's a good idea to use <code>equal?</code> instead of <code>=</code>, in that way your procedure will work for more than just numbers:</p> <pre><code>(define (rmobject list1 obj) (cond ((null? list1) '()) ((equal? obj (car list1)) (rmobject (cdr list1) obj)) (else (cons (car list1) (rmobject (cdr list1) obj))))) </code></pre> <p>For future reference: the procedure you're implementing it's generally included as part of the interpreter. For example, in Racket we have <code>remove*</code>, which uses <code>equal?</code> as the default procedure for testing equality:</p> <pre><code>(define (rmobject list1 obj) (remove* (list obj) list1)) </code></pre> <p>Also, you can use <code>filter</code> as in @Maxwell's answer. Another way to write it:</p> <pre><code>(define (rmobject list1 obj) (filter (negate (curry equal? obj)) list1)) </code></pre> <p>Anyway, this works:</p> <pre><code>(rmobject '(1 2 3 5 0 2 3 5 3) 3) =&gt; '(1 2 5 0 2 5) </code></pre>
    singulars
    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.
    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