duplicates - Remove multiple characters from a list if they are next to each other in Scheme -


i have make dr. racket program removes letters list if following same letter itself. example: (z z f b b d d) become (z f b d). have written code remove first letter list. can help?

#lang racket  (define (remove-duplicates x) (cond ((null? x)      '())     ((member (car x) (cons(car(cdr x)) '())))      (remove-duplicates (cdr x))     (else      (cons (car x) (remove-duplicates (cdr x))))))  (define x '( b c c d d a)) (remove-duplicates x) 

(define (remove-dups x)    (cond      [(empty? x) '()]      [(empty? (cdr x))  (list (car x))]      [(eq? (car x) (cadr x))  (remove-dups (cdr x))]      [else  (cons (car x) (remove-dups (cdr x)))])) 

(cadr x) short (car (cdr x)) in case didn't know.

also, pattern matching makes list deconstruction more readable. in case not much, it's still better other version:

(define (rmv-dups x)   (match x     [(list)  (list)]     [(list a)  (list a)]     [(cons (cons b))  (rmv-dups (cdr x))]     [__  (cons (car x) (rmv-dups (cdr x)))])) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -