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?
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.
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.
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 .
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.
The R5RS, and the R6RS standard library for lists
define memq
, memv
, and member
which can be used for that purpose.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With