问题
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?
回答1:
The R5RS, and the R6RS standard library for lists
define memq
, memv
, and member
which can be used for that purpose.
回答2:
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
回答3:
(define (contains? l i)
(if (empty? l) #f
(or (eq? (first l) i) (contains? (rest l) i))))
回答4:
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.
来源:https://stackoverflow.com/questions/1869116/scheme-built-in-to-check-list-containment