I'm trying to write a function in racket (delete-all xx elt) which returns a new list with all occurrences of elt removed
This is what i have but it only works if the elt appear at the start of the list
(define (delete-all xx elt) (cond ((null? xx) null) ((equal? elt (car xx)) (delete (cdr xx) elt))))
You're missing an extra case: what happens if the current element is not the one you want to delete? Here's the general idea of what needs to be done, I'm not giving you a straight answer because this 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, don't call delete in your answer, given that this is a recursive solution, you need to call delete-all instead, but with appropriate arguments to keep the recursion going until the base case is reached. Hint: what about cons and cdr?