I have a list of elements '(a b c) and I want to find if (true or false) x is in it, where x can be 'a or 'd, for instance. Is there a built in function for this?
相关问题
- Generating powerset in one function, no explicit r
- What is fixed point?
- Forming Lisp code to task — related to flatten lis
- unfold function in scheme
- Confused by Lisp Quoting
相关文章
- Does learning one Lisp help in learning the other?
- What is the definition of “natural recursion”?
- How do I write a macro-defining macro in common li
- How can I unintern a qualified method?
- Changing the nth element of a list
- sleep in emacs lisp
- How do I define a sub environment in scheme?
- Why is it legal in a function definition to make s
You are looking for "find"
Basics - The simplest case is just (find Entry List), usually used as a predicate: "is Entry in List?". If it succeeds in finding the element in question, it returns the first matching element instead of just "t". (Taken from second link.)
http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node145.html
-or-
http://www.apl.jhu.edu/~hall/Lisp-Notes/Higher-Order.html
The procedure return #t (true) or #f (false)
output is #f
If you need to compare using one of the build in equivalence operators, you can use
memq
,memv
, ormember
, depending on whether you want to look for equality usingeq?
,eqv?
, orequal?
, respectively.As you can see, these functions return the sublist starting at the first matching element if they find an element. This is because if you are searching a list that may contain booleans, you need to be able to distinguish the case of finding a
#f
from the case of not finding the element you are looking for. A list is a true value (the only false value in Scheme is#f
) so you can use the result ofmemq
,memv
, ormember
in any context expecting a boolean, such as anif
,cond
,and
, oror
expression.What is the difference between the three different functions? It's based on which equivalence function they use for comparison.
eq?
(and thusmemq
) tests if two objects are the same underlying object; it is basically equivalent to a pointer comparison (or direct value comparison in the case of integers). Thus, two strings or lists that look the same may not beeq?
, because they are stored in different locations in memory.equal?
(and thusmember?
) performs a deep comparison on lists and strings, and so basically any two items that print the same will beequal?
.eqv?
is likeeq?
for almost anything but numbers; for numbers, two numbers that are numerically equivalent will always beeqv?
, but they may not beeq?
(this is because of bignums and rational numbers, which may be stored in ways such that they won't beeq?
)(Note that some behavior of the functions is undefined by the specification, and thus may differ from implementation to implementation; I have included examples that should work in any R5RS compatible Scheme that implements exact rational numbers)
If you need to search for an item in a list using an equivalence predicate different than one of the built in ones, then you may want
find
orfind-tail
from SRFI-1:Here's one way:
member returns everything starting from where the element is, or #f. A cond is used to convert this to true or false.
I don't know if there is a built in function, but you can create one:
Ỳou will get in return the number of occurrences of
x
in the list. you can extend it withtrue
orfalse
too.