Is this for homework, your personal learning, or a work project so I know how much detail to give in future responses if the above isn't enough.

Solved

Posted on 2011-04-19

I need to write a function the removes the second and succeeding adjacent duplicates of string (a b a a a c c).

The result would be (a b a c).

I am new to Scheme and LISP so this is all very confusing to me, expecially recursion.

Any help is appreciated.

The result would be (a b a c).

I am new to Scheme and LISP so this is all very confusing to me, expecially recursion.

Any help is appreciated.

10 Comments

Is this for homework, your personal learning, or a work project so I know how much detail to give in future responses if the above isn't enough.

So I I understand you correctly, I should have a function that looks something like this?

(define (remove-dups a lst)

((equal? a (car lst)) (remove-dups a (cdr (cdr lst))))

(else (remove-dups (car (cdr lst)) (cdr lst))))

As I said I'm knew to Scheme and recursion for that matter. A tough topic to get my hear wrapped around.

You also need to check if the string is empty first (and of course do nothing in that case).

Hers's what I'm thinking for a single parameter:

(define (remove-dups lst)

(cond

((null? lst) THEN WHAT??????)

((equal? (car lst) (car(cdr lst))) (remove-dups(cons (car lst) (cdr(cdr lst)))))

(else(remove-dups(cdr lst)))))

Sure. You could compare car lst to car (cdr lst). If they match, call the function on cdr lst. If they don't match, output car lst and call the function on cdr lst.

Since you keep calling the function on smaller and smaller strings, eventually the string will be empty. In this case, you are done so don't call the function again.

Also, when you say "output car lst", how do you output something? I'm not familiar with any "print" commands in Scheme yet.

Highlights in pink: ((equal? (car lst) -------> (car(cdr lst)) <-------)

car: expects argument of type <pair>; given '()

(define (remove-dups lst)

(cond

((null? lst) '())

((equal? (car lst) (car(cdr lst))) (remove-dups(cons (car lst) (cdr(cdr lst)))))

(else (car lst) (remove-dups(cdr lst)))))

(define (remove-dups2 lst)

(cond

((null? lst) '())

((equal? (car lst) (car(cdr lst))) (remove-dups2(cdr lst)))

(else (car lst) (remove-dups2(cdr lst)))))

Can you take a look at this post please. Thanks for your help.

http://www.experts-exchang

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

static class | 3 | 44 | |

mergeTwo challenge | 13 | 53 | |

Visual Studio unable to copy the file ‘Application.Designer.vb’ from the project template to the project. Access is denied. | 15 | 59 | |

countPairs challenge | 7 | 37 |

This is about my first experience with programming Arduino.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**12** Experts available now in Live!