Changeset 32294 in project


Ignore:
Timestamp:
03/27/15 14:09:41 (5 years ago)
Author:
svnwiki
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/man/4/The R5RS standard

    r30951 r32294  
    864864
    865865== Program structure
     866
     867=== Programs
     868
     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.
     873
     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
     879implementation.)
     880
     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
     887initialization.
     888
     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.
     892
     893=== Definitions
     894
     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>.
     898
     899A definition should have one of the following forms:
     900
     901 (define <variable> <expression>)
     902
     903 (define (<variable> <formals>) <body>)
     904
     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
     909
     910 (define <variable>
     911   (lambda (<formals>) <body>)).
     912
     913 (define (<variable> . <formal>) <body>)
     914
     915 <Formal> should be a single variable. This form is equivalent to
     916
     917 (define <variable>
     918   (lambda <formal> <body>)).
     919
     920==== Top level definitions
     921
     922At the top level of a program, a definition
     923
     924 (define <variable> <expression>)
     925
     926has essentially the same effect as the assignment expression
     927
     928 (set! <variable> <expression>)
     929
     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.
     934
     935 (define add3
     936   (lambda (x) (+ x 3)))
     937 (add3 3)                         ===>  6
     938 (define first car)
     939 (first '(1 2))                   ===>  1
     940
     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.
     945
     946==== Internal definitions
     947
     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
     955example,
     956
     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
     961
     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
     965
     966 (let ((x 5))
     967   (letrec ((foo (lambda (y) (bar x y)))
     968            (bar (lambda (a b) (+ (* a b) a))))
     969     (foo (+ x 3))))
     970
     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
     974defined.
     975
     976Wherever an internal definition may occur (begin <definition1> ...) is
     977equivalent to the sequence of definitions that form the body of the
     978begin.
     979
     980=== Syntax definitions
     981
     982Syntax definitions are valid only at the top level of a
     983<program>. They have the following form:
     984
     985 (define-syntax <keyword> <transformer spec>)
     986
     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.
     990
     991There is no define-syntax analogue of internal definitions.
     992
     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:
     1001
     1002 (define define 3)
     1003
     1004 (begin (define begin list))
     1005
     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)))
    8661016
    8671017== Standard procedures
Note: See TracChangeset for help on using the changeset viewer.