Claudiu
Claudiu

Reputation: 229461

Scheme built-in to check list containment

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?

Upvotes: 5

Views: 14919

Answers (4)

Jérémie Koenig
Jérémie Koenig

Reputation: 1208

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

Upvotes: 7

mqp
mqp

Reputation: 71985

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

Upvotes: 6

Claudiu
Claudiu

Reputation: 229461

(define (contains? l i)
  (if (empty? l) #f
      (or (eq? (first l) i) (contains? (rest l) i))))

Upvotes: 4

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181350

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.

Upvotes: 0

Related Questions