views:

77

answers:

1

For example, take a look at this code (from tspl4):

(define proc1
  (lambda (x y)
    (proc2 y x)))

If I run this as my program in scheme...

#!r6rs
(import (rnrs))

(define proc1
  (lambda (x y)
    (proc2 y x)))

I get this error:

expand: unbound identifier in module in: proc2

...This code works fine though:

#!r6rs
(import (rnrs))

(define proc2
  +)

(define proc1
  (lambda (x y)
    (proc2 y x)))

(display (proc1 2 3)) ;output: 5
+2  A: 

They all have to be defined in the same module (= "library" in r6rs lingo). But you can define them in any order you want -- for example, in your last snip you can swap the two definitions and it will work fine. But note that you cannot put the definitions after the display line -- this is an expression that uses their value, so if you move the function definitions after it, you'll get a runtime error. (Note the fact that it's a runtime error rather than a compile-time one.)

Eli Barzilay
If it's a runtime error, then why does my first program crash? It doens't even run proc1, so why would it need proc2? tspl4 says it shouldn't crash.
Cam
Think about it as a linking problem: PLT actually compiles your code, and you have a reference that the compiler doesn't know about.
Eli Barzilay