begin(7scm)
π begin(7scm) β your lil' wrapper for multiple expressions π
okay so real talk, this page is from the Scheme Programmer's Manual and it's all about begin, which is genuinely one of those keywords that does like three different jobs depending on where you drop it. main character energy fr.
β¨ NAME β¨
begin β wrapper for multiple expressions
π LIBRARY π
like, import it from whichever flavor you're on bestie:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
π¦ SYNOPSIS π¦
(begin expression or definition ...)
(begin expression expression ...)
two shapes, one keyword, no cap.
πΈ DESCRIPTION πΈ
so begin is like that one friend who has a different personality in every group chat. the keyword's role depends on its context:
the splicing era π
it can show up as a form in a body, a library body, or a top-level body, or directly nested inside a begin form that itself appears in a body. in that case the begin form has to have the shape from the first synopsis line.
and this use is a splicing form β the forms inside the body get spliced into the surrounding body, as if the begin wrapper literally was never there. iconic behavior honestly.
β οΈ heads up: a begin form in a body or library body must be non-empty if it appears after the first expression within the body.
the ordinary expression era β¨
it can also appear as an ordinary expression, and then it has to have the shape from the second synopsis line. in this case the expressions get evaluated sequentially, left to right, and the values of the last expression are returned. this is the sequencing era β you use it for side effects like assignments or input and output.
the R7RS plot twist π
in R7RS a third form of begin appears in define-library, where it's used to begin the body of the library. yeah. three eras. we love a versatile queen.
π«Ά RETURN VALUES π«Ά
the values of the last expression are returned, periodt.
but for the splicing form, the rules of wherever the splice appears instead apply. (yes, it defers. it's giving "I'm not the main character here".)
π₯ EXAMPLES π₯
;; This example is poor style but demonstrates the ordering
;; provided by "begin".
(define x 0)
(begin (set! x 5)
(+ x 1)) => 6
;; If "begin" did not guarantee the evaluation order then the
;; above might have evaluated to 1.
like, if begin didn't guarantee ordering that whole thing could've been 1 and we'd all be in shambles π
(begin (display "4 plus 1 equals ")
(display (+ 4 1)))
; prints 4 plus 1 equals 5
it prints 4 plus 1 equals 5. slay.
π APPLICATION USAGE π
- the splicing form of
begindoesn't usually appear in code written by programmers. lowkey it's a macro-output thing. - the sequencing form is sometimes used in conditional expressions β that's a personal style thing. others prefer to use
cond,when, orunless, which makebeginunnecessary. both are valid, it's just vibe check. - in R7RS
define-librarythebeginform is often not used, in favor of usingincludeinstead.
π§ RATIONALE π§
R7RS β this form is commonly used in the output of macros which need to generate multiple definitions and splice them into the context in which they're expanded. it's the macro workhorse, fr fr.
π COMPATIBILITY π
the splicing form of begin does not appear in R4RS and IEEE Scheme. π
the sequencing form enjoys wide compatibility among Lisp dialects β but outside of Scheme it can be named something else: progn in Common Lisp and do in Clojure. iykyk.
R7RS clarifies that the splicing form may appear in the REPL.
π ERRORS π
this form can raise exceptions with the following condition types:
&syntax(R6RS) β raised if the splicing form is used outside of a body, or as a consequence when splicing results in an invalid body. also raised if the keyword is misused.
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. no I'm not being dramatic, that's literally the word.)
π SEE ALSO π
cond(3scm), unless(3scm), when(3scm)
π STANDARDS π
R4RS, IEEE Scheme, R5RS, R6RS, R7RS
β 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/.
k bye bestie π this page is begin(7scm), dated 2020-05-10 β go splice something cute β¨π
π¦ 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