or(7scm)
π or β the short-circuiting vibe check π
okay so this is or(7scm), straight out of the Scheme Programmer's Manual, and bestie it is literally the "first true thing wins" icon we all need. no cap.
β¨ LIBRARY β¨
where you gotta import it from, depending on your whole era:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
π SYNOPSIS π
(or test ...)
that's it. that's the whole flex. π
π¦ DESCRIPTION π¦
so the test expressions get evaluated left to right β like, in order, bestie, one at a time β until one of them hands back a true value, or until you hit the very last test.
if one of them goes true? the whole or expression returns that value and dips out immediately β the remaining expressions do not get evaluated. gone. not today. π«Ά
if none of them go true? then the last expression gets evaluated and its values are what you get back.
and if there are, like, zero tests in there? you get #f. π«
π IMPLEMENTATION NOTES π
some implementations are out here with opinions, and honestly? valid.
- Chez Scheme, Loko Scheme β the source-level optimizer can rewrite code that uses multiple values in one of the test expressions in such a way that the values are no longer in a single-value context. which can mean code that would otherwise raise an exception will run without error, but with possibly unexpected results. this is not a bug, bestie, it's permitted by R6RS. the optimizer is just main character energy. π
- Vicare Scheme β the test expressions are statically checked to see that they return a single value. it's giving strict parent. β
πΈ RETURN VALUES πΈ
returns the value of the first true expression. and remember the golden rule: in Scheme everything is true except for the false value (#f or #false in R7RS). literally everything. a number? true. a list? true. your ex? still true. π
also β the last test expression is in tail context if the or expression itself is. π§
π EXAMPLES π
look at her go:
(or (= 2 2) (> 2 1)) => #t
(or (= 2 2) (< 2 1)) => #t
(or #f #f #f) => #f
(or '(b c) (/ 3 0)) => (b c)
(or) => #f
that fourth one?? '(b c) is true so the (/ 3 0) never runs. no divide-by-zero meltdown. we love a protective queen. π
π APPLICATION USAGE π
the or syntax is very common in almost all programming languages β lowkey everywhere. sometimes it's called or, other times it's && or something else entirely. it's the move when several expressions should be tested but evaluation should not continue past the first true value.
and hey β when your expressions get more complicated, it's better to use cond(3scm). know when to upgrade, bestie. π
β COMPATIBILITY β
the or syntax is widely compatible across Lisp systems. fr fr, it's everywhere.
π₯ ERRORS π₯
this procedure can raise exceptions with the following condition types:
&assertion(R6RS) β zero or more than one value was returned by a non-tail test expression. implementations may also handle this in another way, as long as the safety guarantees of R6RS Β§ 5.6 are upheld. iykyk.- 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. catastrophically. that's the word they used. be careful.
π SEE ALSO π
and(3scm)cond(3scm)if(3scm)
the whole conditional friend group. go say hi. π«Ά
π STANDARDS π
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β she's certified across the board. iconic behavior.
π§ HISTORY π§
short-circuiting evaluation was invented in the 1950s by John McCarthy, the creator of Lisp. yes, the 1950s. the original trendsetter. periodt. β¨
π AUTHORS π
this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more info lives at https://github.com/schemedoc/manpages/.
okay that's the whole page, bestie πΈ β this little love letter was dated 2020-05-03 and it's or(7scm) all the way down. now go short-circuit something responsibly π
β¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2020 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms