Welcome to W3Courses
 Like Us on Facebook

Member

List Member Function using Prolog Source Code

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

CODE
member(X, [X|Y]).
member(X, [H|L]) :- member(X, L).


RESULT
?- member(b, [a,b,c]).
Yes

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