tags:

views:

207

answers:

4

In Python I can do "x in list" to see if the list contains x. Is there any equivalent built-in in Scheme to do this?

A: 

No, there is no list built-in predicate that will do that for you. It's extremely easy to define a lambda or a macro to do just that though.

Pablo Santa Cruz
yeah just annoying
Claudiu
+1  A: 
(define (contains? l i)
  (if (empty? l) #f
      (or (eq? (first l) i) (contains? (rest l) i))))
Claudiu
+5  A: 

The R5RS, and the R6RS standard library for lists define memq, memv, and member which can be used for that purpose.

Jérémie Koenig
It's not just in an SRFI, but also in R5RS (which is implemented by all decent implementations) http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_idx_428
newacct
Thanks for your comment, I have corrected the references in the answer.
Jérémie Koenig
+4  A: 

In PLT Scheme, one has

(member whatever list)
(memv whatever list)
(memq whatever list)

from the SRFI which use, respectively, equal?, eqv?, and eq? to test for equality. There are also a number of other library functions related to searching in lists:

PLT Scheme list reference

mquander