This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: begin at top-level?


Chris Dean wrote:

How come begin returns a different value at the top level? For example:

    #|kawa:2|# (begin 'a 'b)
    ab
    #|kawa:3|# (define (foo) (begin 'a 'b))
    #|kawa:4|# (foo)
    b

Because (begin FORM1 FORM2) is the equivalent to FORM1 followed by FORM2. In a function body, evaluating multiple forms returns the last value. At top level, evaluating multiple forms prints them all - after all it's a read-eval-print loop.


At least that's the logic for how it works in Kawa. Other Scheme implementations are different.

The begin form is kind of funny. It's a pure syntactic grouping form, and is neither a scoping form nor a control form. It is mainly useful for macros, and a few syntax forms like (if ...).

E.g. in:

(begin
  (define a 1)
  (begin
    (begin
      (define b 2)
      (define c 3))
    (define d 4)))

all the variable a, b, c, d are in the same scope. If they were functions they could mutually reference each other.

In the (experimental) KRL and (even more experimenal) Q2, evaluating multiple forms even in a function body returns multiple values.
--
--Per Bothner
per@bothner.com http://per.bothner.com/



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]