member, elem

Scheme's member returns a sub-list starting with the element or #f

(define member (lambda (x xs)
                    (cond ((null? xs) #f) 
                          ((eq? x (car xs)) xs)
                          (else (member x (cdr xs))))))

In Erlang member returns a Boolean

member(X, [X|_]) -> true;
member(X, [_|Y]) -> member(X, Y);
member(X, [])    -> false.

elem returns Bool

(define (elem x xs)
  (any? (partially eq? x) xs))
elem :: (Eq a) => a -> [a] -> Bool
elem _ []      =  False
elem x (y:ys)  =  x==y || elem x ys
member x xs  =  or (map (==x) xs)

in terms of any

elem x                  =  any (== x)
Last modified 3 years ago Last modified on Nov 7, 2016, 12:59:13 PM
Note: See TracWiki for help on using the wiki.