length(3scm)

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

✨ length β€” bestie, how long is your list?? πŸ’…

okay so real talk, this is length(3scm) from the Scheme Programmer's Manual, and it's genuinely one of the most useful little guys in the whole RnRS cinematic universe. come sit with me 🫢


🌸 LIBRARY

import it from wherever your heart (or your Scheme implementation) lives:

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

like, pick your era bestie, all four doors lead to the same function ✨


πŸŽ€ SYNOPSIS

(length list)

πŸ’– DESCRIPTION

Returns the length of list. that's it. that's the whole vibe. it counts. it slays. no cap πŸ’…


πŸ¦‹ IMPLEMENTATION NOTES

so ngl, implementations commonly use something like Floyd's cycle-finding algorithm to double-check that the argument is truly a proper list. yes, that's the tortoise-and-hare thing. the algorithm is literally checking that your list isn't a cursed ouroboros situation πŸ’πŸ‡πŸ”₯


πŸ“ RETURN VALUES

Returns a single exact integer β€” the number of pairs that make up the list. one number, exact, no decimals, no vibes-based estimation. iconic πŸ‘‘


🌈 EXAMPLES

(length '(a b c))          => 3
(length '(a (b) (c d e)))  => 3
(length '())               => 0

see how '(a (b) (c d e)) is still 3?? that's the way that nested lists don't count as extra elements β€” the outer list has three things in it and that's periodt πŸ’€βœ¨


πŸ’œ APPLICATION USAGE

The length procedure often appears in code that deals with an arbitrary number of objects stored in a list, and where the length of the list is needed during serialization. it also tends to show up in code that destructures lists β€” but heads up bestie, in those cases it may be better to use a pattern matching library such as (chibi match). like, length is great, but sometimes you want the fancy tool 🧚


⭐ COMPATIBILITY

  • R6RS requires implementations to verify that the argument is a proper list. very responsible. very "i checked with my mom first" πŸŽ€
  • R7RS does not require this β€” and as a consequence, implementations may misbehave if the argument is not a proper list, or go into a non-terminating loop if the argument is a cyclical list. 😭 that's the danger zone, do not pass go, do not collect $200
lowkey this is the part to remember: cyclical list + R7RS + no checking = infinite loop and you'll be sitting there like πŸ™ˆ

πŸ”₯ 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. rude but fair.
  • 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. yes. catastrophically. πŸ’€ that's the actual word and it's giving main character energy

🌸 SEE ALSO

go read them, iykyk πŸ¦‹


πŸ‘‘ STANDARDS

R4RS, IEEE Scheme, R5RS, R6RS, R7RS β€” like, all of them. it's been that girl for decades ✨


🧚 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/.


okay that's the whole page bestie, stay proper-list out there πŸ«ΆπŸ’–

cute little sign-off: this one's dated 2020-12-31, and yeah length(3scm) is still out here doing the most for the Scheme Programmer's Manual 🌟
πŸ“¦ where this page comes from