I am trying to write a function in racket (delete-all xx elt) that returns a new list with the removal of all occurrences of elt

This is what I have, but it only works if elt appears at the top of the list

(define (delete-all xx elt) (cond ((null? xx) null) ((equal? elt (car xx)) (delete (cdr xx) elt)))) 
+4
source share
2 answers

You are missing an extra case: what happens if the current item is not the one you want to delete? Here's a general idea of ​​what needs to be done, I am not giving you a direct answer, because it looks like homework (you should use the homework tag in your question). Better fill in the blanks yourself:

 (define (delete-all xx elt) (cond ((null? xx) ; base case: empty list null) ; return the empty list ((equal? elt (car xx)) ; current element needs to be removed <???>) ; ignore current element and make recursive call (else ; current element needs to be added to the list (<???> (car xx) <???>)))) ; add current element and make recursive call 

Also, do not call delete in your answer, given that this is a recursive solution, instead you need to call delete-all , but with the appropriate arguments, to preserve the recursion until the base case is reached. Hint: how about cons and cdr ?

+4
source
 (define (delete-all xx elt) remove* xx elt) 
-2
source

Source: https://habr.com/ru/post/1433241/


All Articles