memq(3scm)
π memq β list lookup (yeah, all four of them)
okay bestie, so this is a page from the Scheme Programmer's Manual and it's literally about finding stuff in lists. four little functions, one big vibe. let's go β¨
π«Ά LIBRARY
import these depending on which flavour of Scheme you're living in:
(import (rnrs))β R6RS(import (rnrs lists))β R6RS(import (scheme r5rs))β R7RS(import (scheme base))β R7RS
π SYNOPSIS
(memq obj list)
(member obj list)
(memv obj list)
(memp proc list) ;R6RS
(member obj list compare) ;R7RS
πΈ DESCRIPTION
okay so here's the whole thing, no cap: these procedures return the first sublist of list whose car satisfies a given condition β and "sublists of list" means the lists you get back from (list-tail list k) for k less than the length of list. iconic behaviour.
member, memv and memq are all out here looking for the first occurrence of obj. If list doesn't contain an element satisfying the condition? congrats, you get #f back. π
the difference is literally just the comparison function, and it's giving:
memberusesequal?to compare obj with the elements of listmemvuseseqv?memquseseq?
π¦ memp (R6RS)
memp applies proc to the cars of the sublists of list until it finds one for which proc returns a true value. main character energy, honestly.
some rules bestie, don't come for me:
- proc is always called in the same dynamic environment as
mempitself - proc should accept one argument and return a single value
- proc should not mutate list π
π member (R7RS)
the R7RS member uses compare to compare obj with the elements of list, if given β and equal? otherwise. slay, configurable.
β RETURN VALUES
returns a single value which is either list, a tail of list, or #f. that's it, that's the whole return value situation π
π EXAMPLES
(memq 'a '(a b c))
=> (a b c)
(memq 'b '(a b c))
=> (b c)
(memq 'a '(b c d))
=> #f
(memq (list 'a) '(b (a) c))
=> #f
(member (list 'a) '(b (a) c))
=> ((a) c)
(memq 101 '(100 101 102)) => unspecified
(memv 101 '(100 101 102)) => (101 102)
;; R7RS
(member "B" '("a" "b" "c") string-ci=?)
=> ("b" "c")
;; R6RS
(memp even? '(3 1 4 1 5 9 2 6 5))
=> (4 1 5 9 2 6 5)
not thememq 101returning unspecified π that's your warning right there, bestie β numbers andeq?are NOT it.
π₯ APPLICATION USAGE
these procedures are used to search for an object in a list. you can use them as a simple way to implement sets, and they're commonly used to look up data in property lists. like, genuinely useful, not just cute π«Ά
π COMPATIBILITY
R6RS imposes the following responsibility on implementations (yes, responsibly, that's the original wording, I'm not touching it):
- The implementation must check that list is a chain of pairs up to the found element, or that it is indeed a list if no element is found. It should not check that it is a chain of pairs beyond the found element.
- The implementation must check the restrictions on proc to the extent performed by applying it as described.
- An implementation may check whether proc is an appropriate argument before applying it.
π ERRORS
this procedure can raise exceptions with the following condition types:
&assertion(R6RS) β the wrong number of arguments was passed, or an argument was outside its domain.- R7RS β the assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically. π catastrophic. the drama.
π§ SEE ALSO
eq?(3scm), assq(3scm), find(3scm), exists(3scm)
π STANDARDS
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β she's certified in literally all of them, periodt.
π HISTORY
member and memq appear in MacLISP, so they would have been available in the first Scheme implementation. like, they were there from day one, the OGs. and LISP 1.5 had a member procedure that returned true of the element was in the list.
ngl that last sentence is worded exactly like that in the original β "returned true of the element was in the list" β not my typo, I'm just the messenger π
π AUTHORS
This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.
π¨ BUGS
Programs should not use the more discriminating memq and memv when the object is not comparable with eq?(3scm) and eqv?(3scm), respectively.
fr fr, this is the one that bites people.memqon numbers?memqon strings? don't do it bestie, usememberand live your truth π
okay that's the whole page! this was the memq(3scm) page from the Scheme Programmer's Manual, dated 2021-03-06, and she ate. go find your elements, queen πβ¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2021 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms