r7rs-small-texinfo

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/r7rs-small-texinfo
Log | Files | Refs | README | LICENSE

primitive-expressions.texinfo (9339B)


      1 @node Primitive expression types
      2 @section Primitive expression types
      3 
      4 @menu
      5 * Variable references::
      6 * Literal expressions::
      7 * Procedure calls::
      8 * Procedures::
      9 * Conditionals primitive::
     10 * Assignments::
     11 * Inclusion::
     12 @end menu
     13 
     14 @node Variable references
     15 @subsection Variable references
     16 
     17 @cindex variable
     18 @cindex unbound
     19 
     20 syntax: @svar{variable}
     21 
     22 An expression consisting of a variable
     23 (see @ref{Variables syntactic keywords and regions,,
     24 Variables@comma{} syntactic keywords@comma{} and regions}) is a variable
     25 reference. The value of the variable reference is the value stored in the
     26 location to which the variable is bound. It is an error to reference an
     27 unboundvariable.
     28 
     29 @lisp
     30 (define x 28)
     31 x @result{} 28
     32 @end lisp
     33 
     34 @node Literal expressions
     35 @subsection Literal expressions
     36 
     37 syntax: (quote @svar{datum})
     38 syntax: '@svar{datum}
     39 syntax: @svar{constant}
     40 
     41 @code{(quote} @svar{datum}@code{)} evaluates to @svar{datum}. @svar{Datum} can be any
     42 external representation of a Scheme object
     43 (@xref{External representations basic}). This notation is used to include
     44 literal constants in Scheme code.
     45 
     46 @lisp
     47 (quote a)        @result{} a
     48 (quote #(a b c)) @result{} #(a b c)
     49 (quote (+ 1 2))  @result{} (+ 1 2)
     50 @end lisp
     51 
     52 @code{(quote} @svar{datum}@code{)} can be abbreviated as @code{'}@svar{datum}. The two
     53 notations are equivalent in all respects.
     54 
     55 @lisp
     56 'a         @result{} a
     57 '#(a b c)  @result{} #(a b c)
     58 '()        @result{} ()
     59 '(+ 1 2)   @result{} (+ 1 2)
     60 '(quote a) @result{} (quote a)
     61 ''a        @result{} (quote a)
     62 @end lisp
     63 
     64 Numerical constants, string constants, character constants, vector
     65 constants, bytevector constants, and boolean constants evaluate to
     66 themselves; they need not be quoted.
     67 
     68 @lisp
     69 '145932     @result{} 145932
     70 145932      @result{} 145932
     71 '"abc"      @result{} "abc"
     72 "abc"       @result{} "abc"
     73 '#\a        @result{} #\a
     74 #\a         @result{} #\a
     75 '#(a 10)    @result{} #(a 10)
     76 #(a 10)     @result{} #(a 10)
     77 '#u8(64 65) @result{} #u8(64 65)
     78 #u8(64 65)  @result{} #u8(64 65)
     79 '#t         @result{} #t
     80 #t          @result{} #t
     81 @end lisp
     82 
     83 As noted in @ref{Storage model}, it is an error to attempt to alter a
     84 constant (i.e.@: the value of a literal expression) using a mutation
     85 procedure like @code{set-car!} or @code{string-set!}.
     86 
     87 @node Procedure calls
     88 @subsection Procedure calls
     89 
     90 @cindex call
     91 @cindex procedure call
     92 
     93 syntax: (@svar{operator} @svar{operand@sub{1}} @dots{})
     94 
     95 A procedure call is written by enclosing in parentheses an expression for
     96 the procedure to be called followed by expressions for the arguments to be
     97 passed to it. The operator and operand expressions are evaluated (in an
     98 unspecified order) and the resulting procedure is passed the resulting
     99 arguments.
    100 
    101 @lisp
    102 (+ 3 4)           @result{} 7
    103 ((if #f + *) 3 4) @result{} 12
    104 @end lisp
    105 
    106 The procedures in this document are available as the values of variables
    107 exported by the standard libraries. For example, the addition and
    108 multiplication procedures in the above examples are the values of the
    109 variables @code{+} and @code{*} in the base library. New procedures are
    110 created by evaluating lambda expressions (@xref{Procedures}).
    111 
    112 Procedure calls can return any number of values (see values in @ref{Control
    113 features}). Most of the procedures defined in this report return one value
    114 or, for procedures such as apply, pass on the values returned by a call to
    115 one of their arguments. Exceptions are noted in the individual descriptions.
    116 
    117 Note: In contrast to other dialects of Lisp, the order of evaluation is
    118 unspecified, and the operator expression and the operand expressions are
    119 always evaluated with the same evaluation rules.
    120 
    121 Note: Although the order of evaluation is otherwise unspecified, the effect
    122 of any concurrent evaluation of the operator and operand expressions is
    123 constrained to be consistent with some sequential order of evaluation. The
    124 order of evaluation may be chosen differently for each procedure call.
    125 
    126 Note: In many dialects of Lisp, the empty list, @code{()}, is a legitimate
    127 expression evaluating to itself. In Scheme, it is an error.
    128 
    129 @node Procedures
    130 @subsection Procedures
    131 
    132 @deffn syntax lambda @svar{formals} @svar{body}
    133 
    134 Syntax: @svar{Formals} is a formal arguments list as described below, and
    135 @svar{body} is a sequence of zero or more definitions followed by one or
    136 more expressions.
    137 
    138 Semantics: A @code{lambda} expression evaluates to a procedure. The
    139 environment in effect when the @code{lambda} expression was evaluated is
    140 remembered as part of the procedure. When the procedure is later called with
    141 some actual arguments, the environment in which the @code{lambda} expression
    142 was evaluated will be extended by binding the variables in the formal
    143 argument list to fresh locations, and the corresponding actual argument
    144 values will be stored in those locations. (A @define{fresh} location is one
    145 that is distinct from every previously existing location.) Next, the
    146 expressions in the body of the @code{lambda} expression (which, if it
    147 contains definitions, represents a @code{letrec*} form---@xref{Binding
    148 constructs}) will be evaluated sequentially in the extended environment. The
    149 results of the last expression in the body will be returned as the results
    150 of the procedure call.
    151 
    152 @lisp
    153 (lambda (x) (+ x x)) @result{} @r{a procedure}
    154 ((lambda (x) (+ x x)) 4) @result{} 8
    155 @end lisp
    156 
    157 @lisp
    158 (define reverse-subtract
    159   (lambda (x y) (- y x)))
    160 (reverse-subtract 7 10) @result{} 3
    161 
    162 (define add4
    163   (let ((x 4))
    164     (lambda (y) (+ x y))))
    165 (add4 6) @result{} 10
    166 @end lisp
    167 
    168 @svar{Formals} have one of the following forms:
    169 
    170 @itemize
    171 @item
    172 @code{(}@svar{variable@sub{1}} @dots{}@code{)}: The procedure takes a fixed
    173 number of arguments; when the procedure is called, the arguments will be
    174 stored in fresh locations that are bound to the corresponding variables.
    175 
    176 @item
    177 @svar{variable}: The procedure takes any number of arguments; when the
    178 procedure is called, the sequence of actual arguments is converted into a
    179 newly allocated list, and the list is stored in a fresh location that is
    180 bound to @svar{variable}.
    181 
    182 @item
    183 @code{(}@svar{variable@sub{1}} @dots{} @svar{variable@sub{n}} .
    184 @svar{variable@sub{n+1}}@code{)}: If a space-delimited period precedes the
    185 last variable, then the procedure takes @var{n} or more arguments, where
    186 @var{n} is the number of formal arguments before the period (it is an error
    187 if there is not at least one). The value stored in the binding of the last
    188 variable will be a newly allocated list of the actual arguments left over
    189 after all the other actual arguments have been matched up against the other
    190 formal arguments.
    191 
    192 @end itemize
    193 
    194 It is an error for a @svar{variable} to appear more than once in @svar{formals}.
    195 
    196 @lisp
    197 ((lambda x x) 3 4 5 6) @result{} (3 4 5 6)
    198 ((lambda (x y . z) z)
    199  3 4 5 6)              @result{} (5 6)
    200 @end lisp
    201 
    202 Each procedure created as the result of evaluating a @code{lambda}
    203 expression is (conceptually) tagged with a storage location, in order to
    204 make @code{eqv?} and @code{eq?} work on procedures (@xref{Equivalence
    205 predicates}).
    206 @end deffn
    207 
    208 @node Conditionals primitive
    209 @subsection Conditionals
    210 
    211 @cindex true
    212 
    213 @deffn  syntax if @svar{test} @svar{consequent} @svar{alternate}
    214 @deffnx syntax if @svar{test} @svar{consequent}
    215 
    216 Syntax: @svar{Test}, @svar{consequent}, and @svar{alternate} are expressions.
    217 
    218 Semantics: An @code{if} expression is evaluated as follows: first,
    219 @svar{test} is evaluated. If it yields a true value (@xref{Booleans}), then
    220 @svar{consequent} is evaluated and its values are returned. Otherwise
    221 @svar{alternate} is evaluated and its values are returned. If @svar{test}
    222 yields a false value and no @svar{alternate} is specified, then the result
    223 of the expression is unspecified.
    224 @end deffn
    225 
    226 @lisp
    227 (if (> 3 2) 'yes 'no) @result{} yes
    228 (if (> 2 3) 'yes 'no) @result{} no
    229 (if (> 3 2)
    230     (- 3 2)
    231     (+ 3 2))          @result{} 1
    232 @end lisp
    233 
    234 @node Assignments
    235 @subsection Assignments
    236 
    237 @cindex region
    238 
    239 @deffn syntax set! @svar{variable} @svar{expression}
    240 
    241 Semantics: @svar{Expression} is evaluated, and the resulting value is stored
    242 in the location to which @svar{variable} is bound. It is an error if
    243 @svar{variable} is not bound either in some regionenclosing the set!
    244 expression or else globally. The result of the @code{set!} expression is
    245 unspecified.
    246 @end deffn
    247 
    248 @lisp
    249 (define x 2)
    250 (+ x 1)      @result{} 3
    251 (set! x 4)   @result{} @r{unspecified}
    252 (+ x 1)      @result{} 5
    253 @end lisp
    254 
    255 @node Inclusion
    256 @subsection Inclusion
    257 
    258 @deffn  syntax include @svar{string@sub{1}} @svar{string@sub{2}}@dots{}
    259 @deffnx syntax include-ci @svar{string@sub{1}} @svar{string@sub{2}}@dots{}
    260 
    261 Semantics: Both @code{include} and @code{include-ci} take one or more
    262 filenames expressed as string literals, apply an implementation-specific
    263 algorithm to find corresponding files, read the contents of the files in the
    264 specified order as if by repeated applications of read, and effectively
    265 replace the @code{include} or @code{include-ci} expression with a
    266 @code{begin} expression containing what was read from the files. The
    267 difference between the two is that @code{include-ci} reads each file as if
    268 it began with the @code{#!fold-case} directive, while @code{include} does
    269 not.
    270 
    271 Note: Implementations are encouraged to search for files in the directory
    272 which contains the including file, and to provide a way for users to specify
    273 other directories to search.
    274 @end deffn