list-sort(3scm)
β¨ list-sort, vector-sort β stable sorting of lists and vectors β¨
okay bestie, you know how sorting is like, the most main-character thing in all of programming? yeah. this is that. this is list-sort(3scm) from the Scheme Programmer's Manual and it's giving organized chaos. π
π LIBRARY π
so like, you gotta import these bad boys first, iykyk:
(import (rnrs)) ;R6RS
(import (rnrs sorting)) ;R6RS
π¦ SYNOPSIS π¦
here's the whole vibe, the shape of it:
(list-sort proc list)
(vector-sort proc vector)
two functions. one for lists, one for vectors. iconic behavior. π
πΈ DESCRIPTION πΈ
so list-sort and vector-sort perform a stable sort of your list or vector in ascending order according to proc. like, they take your messy little collection and hand it back to you all glowing up. β¨
the proc is the rulebook, okay? it should accept two elements and return a true value when its first argument is strictly less than its second, and #f otherwise. that's the whole job. that's it. easy. π«Ά
β±οΈ Time complexity β±οΈ
the sorting algorithm performs O(n lg n) calls to proc, where n is the length of list or vector. no cap, that's the complexity. ngl that's pretty slay of them.
π Side effects π
literally do NOT giveprocside effects bestie. the pairing of arguments and the sequencing of calls toprocare not specified β so like, you don't get to control which elements get compared when, k? the algorithm is the main character here, not you. π
good news though: the sorting algorithm does not modify list or vector. your original data is safe and untouched. periodt. π
π§ IMPLEMENTATION NOTES π§
okay this part is such tea. β
- Quicksort cannot be used because it has worse time complexity than what is required.
- Heapsort cannot be used either because it is not stable. π
- Merge sort satisfies all requirements and can be used β but it is not the only option.
so like, merge sort is the safe pick, but the door's open if you're feeling creative. slay.
π RETURN VALUES π
both of these procedures return a single value. the list-sort procedure returns a list, and vector-sort returns a vector. classic.
the return value may be eq? to the argument when the argument is already sorted (like, if it's already perfect, they just hand it back to you β generous queens π), and the list returned by list-sort may share structure with a tail of the original list. so like, heads up, they're close. π«Ά
also: if multiple returns occur from list-sort or vector-sort, the return values returned by earlier returns are not mutated. good behavior. we stan. π
π₯ EXAMPLES π₯
honestly? study these. they ate:
(list-sort < '(3 5 2 1)) => (1 2 3 5)
(vector-sort < '#(3 5 2 1)) => #(1 2 3 5)
see how 3 5 2 1 becomes 1 2 3 5? that's the work. that's literally the work. β¨
π COMPATIBILITY π
these procedures are unique to R6RS. and if you're out here like "but bestie what about alternatives," don't worry, they're available in SRFI-95 and SRFI-132. you got options, iykwim. π
π 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. in particular,procshould accept two arguments, which are elements fromlistorvector, and return one value. like, don't play with it. the vibe check will fail. π
π SEE ALSO π
vector-sort!(3scm) β the sibling, the one with the bang. go say hi. π
β STANDARDS β
R6RS. that's the standard, bestie. it's giving official. π
π HISTORY π
these procedures first appeared in R6RS. so they're not ancient, but they're also not new. they're just... them. π
π 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 check it out fr fr. π«Ά
π
that's a wrap, bestie! list-sort(3scm) β Scheme Programmer's Manual, dated 2023-01-25. sort responsibly. β¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms