list?(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’– list? β€” check if an object is a list πŸ’–

okay so bestie, welcome to list?(3scm), the Scheme Programmer's Manual page that is genuinely about to make you feel so much better about your code. let's get into it ✨


🌸 LIBRARY 🌸

like, where do you even import this from? here's the whole lineup, no cap:

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS
(import (scheme r5rs))              ;R7RS
(import (scheme base))              ;R7RS

pick your vibe, they all got you 🫢


✨ SYNOPSIS ✨

(list? obj)

that's it. that's the whole call. iconic behavior honestly πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so list? returns #t if obj is a list, and #f otherwise. boom. done. but wait, what even is a list, you ask? by definition, all lists are chains of pairs that have finite length and are terminated by the empty list. literally the definition. write it down πŸ“


🧚 IMPLEMENTATION NOTES 🧚

okay this part is lowkey cool. this procedure is expected to run in time linear to the number of elements in the list, which, slay. and implementations can be expected to use an algorithm like Floyd's cycle-finding algorithm β€” aka "tortoise and hare" πŸ’πŸ‡ β€” which handles the case of circular lists created with set-cdr!(3scm). so like, if you made a cursed little ouroboros of a list, this thing still knows what's up. πŸ”₯


πŸŽ€ RETURN VALUES πŸŽ€

returns a boolean object. #t or #f. no in-between, no "it's complicated", no situationship. πŸ’€


🌈 EXAMPLES 🌈

here's where it gets fun, bestie:

(list? '(a b c))  => #t

(list? '())       => #t

(list? '(a . b))  => #f

(let ((x (list 'a)))
  (set-cdr! x x)
  (list? x))      => #f

see that last one?? that's the circular list situation, and list? said "not today" 😭 periodt.


πŸ’œ COMPATIBILITY πŸ’œ

the list? procedure is identical in all revisions of the Scheme reports (we love consistency πŸ’–). and IEEE Scheme explicitly mentions the case of circular structures β€” so yeah, this was foreseen. πŸ‘‘


πŸ“ ERRORS πŸ“

this procedure can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” the wrong number of arguments was passed. like, you gave it too many or too few. read the synopsis bestie, it takes one 😀
  • 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. πŸ’€πŸ’€πŸ’€ yeah. catastrophic failure. no big deal. (it's a big deal.)

πŸ”₯ SEE ALSO πŸ”₯

  • pair?(3scm) β€” the other one. you'll want it. trust. iykyk πŸ’…

⭐ STANDARDS ⭐

R4RS, IEEE Scheme, R5RS, R6RS, R7RS

all of them. every single one. ate and left no crumbs. πŸŽ€


🌸 HISTORY 🌸

the list? procedure first appeared in R4RS. baby's first appearance πŸ₯ΉπŸ¦‹


πŸ‘‘ 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/. go give them your love 🫢✨


πŸ’€ BUGS πŸ’€

ngl, there's one mistake that is fairly common: calling list? when you're destructuring an object β€” like, immediately before using car(3scm) or cdr(3scm). don't do that. πŸ™ˆ use pair?(3scm) instead. that's the whole note. save it to your notes app fr fr.


okay that's the page!! stay curious, stay caffeinated, and please stop making circular lists with set-cdr! unless you really mean it πŸ˜­πŸ’–

β€” cute little sign-off: this page is dated 2022-03-23 and it belongs to list?(3scm), mwah πŸ’‹

πŸ“¦ where this page comes from