We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Scheme Remove Duplicates From List Question

Medium Priority
3,471 Views
1 Endorsement
Last Modified: 2012-05-11

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

Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
should ((null? (cdr lst)) '())
be ((null? (cdr lst)) (car lst))

Author

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

Author

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.
CERTIFIED EXPERT
Top Expert 2011

Commented:
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.
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.

Author

Commented:
this is the error I get:
cond: expected a clause with one question and one answer, but found a clause with 3 parts

Author

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)))
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
So is the whole thing working now?

Author

Commented:
Yes, it is working.
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
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).

Author

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.
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.