Changeset 32294 in project

03/27/15 14:09:41 (5 years ago)

Anonymous wiki edit for IP []: Add missing Program Structure section

1 edited


  • wiki/man/4/The R5RS standard

    r30951 r32294  
    865865== Program structure
     867=== Programs
     869A Scheme program consists of a sequence of expressions, definitions,
     870and syntax definitions. Expressions are described in chapter 4;
     871definitions and syntax definitions are the subject of the rest of the
     872present chapter.
     874Programs are typically stored in files or entered interactively to a
     875running Scheme system, although other paradigms are possible;
     876questions of user interface lie outside the scope of this
     877report. (Indeed, Scheme would still be useful as a notation for
     878expressing computational methods even in the absence of a mechanical
     881Definitions and syntax definitions occurring at the top level of a
     882program can be interpreted declaratively. They cause bindings to be
     883created in the top level environment or modify the value of existing
     884top-level bindings. Expressions occurring at the top level of a
     885program are interpreted imperatively; they are executed in order when
     886the program is invoked or loaded, and typically perform some kind of
     889At the top level of a program (begin <form1> ...) is equivalent to the
     890sequence of expressions, definitions, and syntax definitions that form
     891the body of the begin.
     893=== Definitions
     895Definitions are valid in some, but not all, contexts where expressions
     896are allowed. They are valid only at the top level of a <program> and
     897at the beginning of a <body>.
     899A definition should have one of the following forms:
     901 (define <variable> <expression>)
     903 (define (<variable> <formals>) <body>)
     905<Formals> should be either a sequence of zero or more variables, or a
     906sequence of one or more variables followed by a space-delimited period
     907and another variable (as in a lambda expression). This form is
     908equivalent to
     910 (define <variable>
     911   (lambda (<formals>) <body>)).
     913 (define (<variable> . <formal>) <body>)
     915 <Formal> should be a single variable. This form is equivalent to
     917 (define <variable>
     918   (lambda <formal> <body>)).
     920==== Top level definitions
     922At the top level of a program, a definition
     924 (define <variable> <expression>)
     926has essentially the same effect as the assignment expression
     928 (set! <variable> <expression>)
     930if <variable> is bound. If <variable> is not bound, however, then the
     931definition will bind <variable> to a new location before performing
     932the assignment, whereas it would be an error to perform a set! on an
     933unbound variable.
     935 (define add3
     936   (lambda (x) (+ x 3)))
     937 (add3 3)                         ===>  6
     938 (define first car)
     939 (first '(1 2))                   ===>  1
     941Some implementations of Scheme use an initial environment in which all
     942possible variables are bound to locations, most of which contain
     943undefined values. Top level definitions in such an implementation are
     944truly equivalent to assignments.
     946==== Internal definitions
     948Definitions may occur at the beginning of a <body> (that is, the body
     949of a lambda, let, let*, letrec, let-syntax, or letrec-syntax
     950expression or that of a definition of an appropriate form). Such
     951definitions are known as internal definitions as opposed to the top
     952level definitions described above. The variable defined by an internal
     953definition is local to the <body>. That is, <variable> is bound rather
     954than assigned, and the region of the binding is the entire <body>. For
     957 (let ((x 5))
     958   (define foo (lambda (y) (bar x y)))
     959   (define bar (lambda (a b) (+ (* a b) a)))
     960   (foo (+ x 3)))                        ===>  45
     962A <body> containing internal definitions can always be converted into
     963a completely equivalent letrec expression. For example, the let
     964expression in the above example is equivalent to
     966 (let ((x 5))
     967   (letrec ((foo (lambda (y) (bar x y)))
     968            (bar (lambda (a b) (+ (* a b) a))))
     969     (foo (+ x 3))))
     971Just as for the equivalent letrec expression, it must be possible to
     972evaluate each <expression> of every internal definition in a <body>
     973without assigning or referring to the value of any <variable> being
     976Wherever an internal definition may occur (begin <definition1> ...) is
     977equivalent to the sequence of definitions that form the body of the
     980=== Syntax definitions
     982Syntax definitions are valid only at the top level of a
     983<program>. They have the following form:
     985 (define-syntax <keyword> <transformer spec>)
     987<Keyword> is an identifier, and the <transformer spec> should be an
     988instance of syntax-rules. The top-level syntactic environment is
     989extended by binding the <keyword> to the specified transformer.
     991There is no define-syntax analogue of internal definitions.
     993Although macros may expand into definitions and syntax definitions in
     994any context that permits them, it is an error for a definition or
     995syntax definition to shadow a syntactic keyword whose meaning is
     996needed to determine whether some form in the group of forms that
     997contains the shadowing definition is in fact a definition, or, for
     998internal definitions, is needed to determine the boundary between the
     999group and the expressions that follow the group. For example, the
     1000following are errors:
     1002 (define define 3)
     1004 (begin (define begin list))
     1006 (let-syntax
     1007   ((foo (syntax-rules ()
     1008           ((foo (proc args ...) body ...)
     1009            (define proc
     1010              (lambda (args ...)
     1011                body ...))))))
     1012   (let ((x 3))
     1013     (foo (plus x y) (+ x y))
     1014     (define foo x)
     1015     (plus foo x)))
    8671017== Standard procedures
Note: See TracChangeset for help on using the changeset viewer.