Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme built-in to check list containment

Tags:

list

scheme

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?

like image 910
Claudiu Avatar asked Dec 08 '09 19:12

Claudiu


People also ask

How do I check for an empty list in Scheme?

Scheme provides a procedure, null? to check whether a value is (a pointer to) the empty list, i.e., a null pointer. For example, (null? foo) returns #t if the value of the variable foo is the empty list, and #f otherwise.

What is a list in Scheme?

In contrast to Scheme's unstructured data types, such as symbols and numbers, lists are structures that contain other values as elements. A list is an ordered collection of values. In Scheme, lists can be heterogeneous, in that they may contain different kinds of values.

How does append work in Scheme?

The append function joins two lists together to make one. The append function is built into Scheme. It concatenates two lists, that is to say, given two lists list1 and list2 it produces a new list which starts with the same elements as list1 and finishes with those of list2 .

What does cons do in Scheme?

The other constructor, cons , is used when you already have a list and you want to add one new element. Cons takes two arguments, an element and a list (in that order), and returns a new list whose car is the first argument and whose cdr is the second.


2 Answers

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

like image 181
Jérémie Koenig Avatar answered Nov 15 '22 07:11

Jérémie Koenig


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

like image 33
mqp Avatar answered Nov 15 '22 07:11

mqp