Logo

Programming-Idioms

History of Idiom 12 > diff from v100 to v101

Edit summary for version 101 by ji:
[Scheme] Wasn't good

Version 100

2020-04-06, 22:43:39

Version 101

2020-04-06, 22:44:42

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)
    (if
        ((null? list) #f)
        ((equal? (car list) x) #t)
        ((contains (cdr list) x))))
Code
(define (contains list x)
        (if (null? list) #f)
        (if (equal? (car list) x) #t)
        (else (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