Scheme Remove Duplicates From List Question


Hi,

I'm supposed to use Scheme, with the Dr. Racket program, to remove duplicates from a list.
I have the list (a b a a a a c c c) and I need to get the result (a b a c).

The code I have so far gives me (a b a)

(define (removeduplicate lst)
  
    
    
  (cond
    
    ((null? lst) '()) 
    
    
    
    ((null? (cdr lst)) '()) 
    
    
    
    ((equal? (car lst)(car(cdr lst)))
              (removeduplicate(cdr lst)))
    
    
    (else (cons(car lst)(removeduplicate(cdr lst))))
      
  ))

Open in new window

TechLearnerCAAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TommySzalapskiConnect With a Mentor Commented:
((null? (cdr lst)) '())
This is true when you only have one character left. At this point you will always want to print out that character (car(lst)) since it will always exist and always not be null. You actually don't need the ((null? lst) '())  line except for hadling the case where you are given a null string.
0
 
ozoCommented:
should ((null? (cdr lst)) '())
be ((null? (cdr lst)) (car lst))
0
 
TechLearnerCAAuthor Commented:
If I make that change I get the following error message:
cons: second argument must be of type <list or cyclic list>, given 'a and 'c

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
TechLearnerCAAuthor Commented:
One thing I noticed is that each element is compared to the next element. Eventually the last c in the list needs to be compared to null. Since null and c are not equal, cons should add c to the list. But it does not. I'm not sure how to remedy this problem.
0
 
jimyXCommented:
I don't use LISP (never used it) but when reading your last comment, I can think of work around solution which is to add a dummy item to be at the end of the list such as '#' or any unique item that will not be mentioned in this list so it gets ignored rather than the actual intended last item.

Just a suggestion if you want to give it a try.
0
 
TechLearnerCAAuthor Commented:
How do I print out the last character (car lst) and add that to the list? When I put ((null? (cdr lst)) cons(car lst)) I get an error.
0
 
TechLearnerCAAuthor Commented:
this is the error I get:
cond: expected a clause with one question and one answer, but found a clause with 3 parts
0
 
TechLearnerCAAuthor Commented:
Solved the problem. For cons the second item added must be a list. So I converted car lst to a list by doing this: ((null?(cdr lst)) (list(car lst)))
0
 
TommySzalapskiCommented:
So is the whole thing working now?
0
 
TechLearnerCAAuthor Commented:
Yes, it is working.
0
 
TommySzalapskiCommented:
In http:#a35452747, I gave the hint that got him to the solution. I'm not sure why he had an error since he didn't describe the error, but the idea was correct and did lead him to the solution (as far as I can tell).
0
 
TechLearnerCAAuthor Commented:
TommySzalapski, you did not get me to the solution. In fact you were no help at all. Just like every other 'Expert' on message boards like this.
0
 
DhaestCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
All Courses

From novice to tech pro — start learning today.