0

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Cam
  • 14,930
  • 16
  • 77
  • 128

1 Answers1

2

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
  • 29,301
  • 3
  • 67
  • 110
  • 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 Jun 04 '10 at 20:37
  • 1
    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 Jun 04 '10 at 20:45