Welcome to W3Courses
 Like Us on Facebook

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)))))))

Share

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

Share

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!

Share

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!

Share

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


 

Share

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)))))))

Share