[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Scheme BNF



    Date: Sat, 29 Mar 86 17:41:34 EST
    From: Jonathan A Rees <JAR@MC.LCS.MIT.EDU>

				 ...  But in order to allow (BEGIN) to work,
    we have to add an exception to the description "BEGIN evaluates the
    forms sequentially, and returns the value of the last form" - you have
    to say "...except that if there are no forms, the value is unspecified".
    I have written interpreters and compilers that deal with (BEGIN) and
    they always have an extra conditional in them to be able to deal with
    that as a special case. ...

The problem is with the form of the English; it blithely assumes that
there is a last form.  This is actually a fencepost error; we should
focus on the spaces between the forms, and not the forms themselves.
After the word BEGIN there is an <unspecified>, and after each subform
the value of that form appears.  BEGIN forms a sequence r of the
<unspecified> value and the values of all the forms, and then returns
(reduce #'(lambda (x y) y) r).  Clear?

To render this as a DO loop:

(define (interpret-begin begin-form environment)
  (do ((forms (cdr begin-form) (cdr forms))
       (result <unspecified> (interpret-form (car forms) environment)))
      ((null forms) result)))

See?  No special cases.
--Guy