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
2009-12-08 19:06:45
yeah just annoying
Claudiu
2009-12-08 19:14:03
+1
A:
(define (contains? l i)
(if (empty? l) #f
(or (eq? (first l) i) (contains? (rest l) i))))
Claudiu
2009-12-08 19:14:12
+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
2009-12-08 19:32:35
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
2009-12-09 05:54:15
Thanks for your comment, I have corrected the references in the answer.
Jérémie Koenig
2009-12-09 17:56:53
+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:
mquander
2009-12-08 19:33:10