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
?
You could also use filter
, that is if you're allowed to use higher order functions :
(define (delete-all xx elt)
(filter (lambda (y) (not(eq? xx y))) elt))
(define (delete-all xx elt)
remove* xx elt)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.