Logo

Programming-Idioms

History of Idiom 12 > diff from v99 to v100

Edit summary for version 100 by ji:
[Scheme] Wasn't correct

Version 99

2020-04-06, 21:34:15

Version 100

2020-04-06, 22:43:39

Idiom #12 Check if list contains a value

Check if list contains a value x.
list is an iterable finite container.

Illustration

Idiom #12 Check if list contains a value

Check if list contains a value x.
list is an iterable finite container.

Illustration
Extra Keywords
array vector member membership
Extra Keywords
array vector member membership
Code
(define (contains list x)
    (cond 
        ((null? list) #f)
        ((eq? (car list) x) #t)
        (else (contains (cdr list) x))))
Code
(define (contains list x)
    (if
        ((null? list) #f)
        ((equal? (car list) x) #t)
        ((contains (cdr list) x))))
Comments bubble
This is a custom implementation, use it if no built-in predicate is available in your Scheme environment.
Comments bubble
This is a custom implementation, use it if no built-in predicate is available in your Scheme environment.
Demo URL
http://repl.it/SIT
Demo URL
http://repl.it/SIT