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))))

Answers


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?


Need Your Help

How to make height of overlay depend on height of textview?

android android-layout

I'm trying to make the height of an overlay depend on the weight of a textview. As below, I have an image in the background and a semi-transparent overlay that acts as the background for some text....

Delphi search for files in a thread

multithreading delphi delphi-2010 findfirst

Got this pretty straight forward function to search for files:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.