Question

Consider the following code:

#!r6rs
(library
 (test)
 (export)
 (import (rnrs))

 (define a 5)
 (begin
   (define b 4)
   (+ 3 b))
 'cont
 (define c 5)
 'done)

From the R6RS Report 7.1:

A <library body> is like a <body> (see section 11.3) except that a <library body>s need not include any expressions. It must have the following form:

<definition> ... <expression> ...

I thought it would emit error because definition of c is after expression 'cont, but this program is accepted cleanly.

After Then, I thought a and c could be exported. But, not c but b can be exported. (a can be exported as I thought.)

I think there are something I didn't realize about R6RS library rules. What is the point I'm missing? Thanks in advance.

p.s) I'm using Racket v5.3.3

Was it helpful?

Solution

From the R6RS 2007 specification:

A library definition must have the following form:

(library <library name>
  (export <export spec> ...)

  (import <import spec> ...)

  <library body>)


...

 The <library body> is the library body, consisting of a sequence of definitions 
 followed by a sequence of expressions. The definitions may be both for local 
 (unexported) and exported bindings, and the expressions are initialization 
 expressions to be evaluated for their effects.

Thus, for your example code, an error should have been raised.

OTHER TIPS

Sorry, this is not the right answer. It is how the program toplevel works, not library toplevel. Leaving it here for reference.

In the program toplevel things work a bit different from normal (normal being the way you interpreted it).

The code will be rewritten by the compiler to look something like:

(define a 5)
(define b 4)
(define dummy1 (+ 3 b))
(define dummy2 'cont)
(define c 5)
'done

Notes:

  • begin splices in the toplevel
  • For any non-definition, the expression is assigned to a 'dummy' variable
  • The toplevel eventually ends up looking like a letrec* and the same rules apply
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top