Idiom #12 Check if list contains a value
Check if the list contains the value x.
list is an iterable finite container.

- Ada
- C
- Caml
- Clojure
- Clojure
- C++
- C++
- C#
- D
- Dart
- Elixir
- Elixir
- Erlang
- Erlang
- Erlang
- Fortran
- Fortran
- Go
- Go
- Groovy
- Haskell
- Haskell
- JS
- JS
- Java
- Java
- Java
- Java
- Kotlin
- Kotlin
- Lisp
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Pascal
- Pascal
- Perl
- Perl
- Prolog
- Ruby
- Rust
- Rust
- Rust
- Rust
- Scala
- Scheme
- Scheme
- Smalltalk
- VB
return list.indexOf(x) !== -1;
Array.prototype.includes() is preferred but if you are supporting browsers that are 5+ years old, for example IE11, and you are not using a transpiler, then the old syntax with indexOf is generally well-understood.
print "Found 'foo'\n" if grep {$_ eq $x} @list;
The inside of the curly braces for a hash(map) reference is automatically stringified.
(define (contains list x)
(cond [(null? list) #f]
[(equal? (car list) x) #t]
[else (contains (cdr list) x)]))
This is a custom implementation, use it if no built-in predicate is available in your Scheme environment.