Welcome to W3Courses
 Like Us on Facebook

Delete the Third Occurrence of an Element from a List Using Scheme Source Code

The following code deletes the third occurrence of an element from a list

CODE
 (define z 1)
 (define (del3 x y)
       (cond ((null? y) '())
            ((eqv? x (car y)) (cond ((< z 3) (set! z (+ z 1)) (cons (car y) (del3 x (cdr y))))
                (else (set! z 1) (del3 x (cdr y)))))
            (else (cons (car y) (del3 x (cdr y))))))


RESULT
> (del3 'a '(a b c a a d))
(a b c a d)


 

Share