This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: begin at top-level?
- From: Per Bothner <per at bothner dot com>
- To: Chris Dean <ctdean at sokitomi dot com>
- Cc: Kawa List <kawa at sources dot redhat dot com>
- Date: Tue, 11 Jan 2005 13:46:50 -0800
- Subject: Re: begin at top-level?
- References: <m2y8ezlnlx.fsf@tungsten.mercedsystems.com>
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/