Welcome to W3Courses

Element

Delete all Occurrences of an Element from a List Using Scheme Source Code

The following code deletes all occurrences of an element from a list

CODE
 (define deleteall( lambda (x y)
      (if (null? y )
            ()
      (if (eqv? (car y) x)
           (deleteall x (cdr y))
      (cons (car y) (deleteall x (cdr y)))))))

1.5
Average: 1.5 (2 votes)

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

1
Average: 1 (1 vote)

Delete the Nth Occurrence of an Element from a List with Set! Using Scheme Source Code

The following code deletes the Nth occurrence of an element from a List with Set!

3
Average: 3 (1 vote)

Delete the Nth Occurrence of an Element from a List without Set! Using Scheme Source Code

The following code deletes the Nth occurrence of an element from a list without using Set!

0

Check if an Element is a Member of a List using Scheme Source Code

The following code checks if an element is a member of a list

CODE
(define (member? x y)
      (cond ((null? y) #f)
((eqv? x (car y)) #t)
             (else (member? x (cdr y)))))


RESULT
> (member_list 'd '(a x c v b))
#f
> (member? 'f  '(a s d f g h))
#t


 

0

Replace an Element from a List using Scheme Source Code

The following code replaces an element from a list

CODE
(define replace( lambda (x b y)
      (if (null? y )
             ()
      (if (eqv? (car y) x)
             (cons b (replace x b (cdr y)))
      (cons (car y) (replace x b (cdr y)))))))

0