[plt-scheme] reference before it's defined

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu Apr 29 12:52:29 EDT 2004

You need to define the function before it is used, but not before you
refer to it (a common problem: be sure to put all of your test cases at
the end of the file).

Probably the most helpful thing is to try one of the confusing examples
in the stepper. 

Here's one (silly) example to try out in the stepper:

;; even-num? : natural-number -> boolean
(define (even-num? n)
  (cond
    [(zero? n) true]
    [else (odd-num? (sub1 n))]))

;; odd-num? : natural-number -> boolean
(define (odd-num? n)
  (cond
    [(zero? n) false]
    [else (even-num? (sub1 n))]))

Those two functions clearly refer to each other, but since they haven't
been called yet, everything is fine.

If you were to put a call to even-num? (for example, with 7 as the
input) in between the two functions, then you would get the error,
because even? calls odd? but odd? hadn't been defined (yet). If you put
the call to even? after the definition of odd?, everything will be as
expected.

Robby

At Thu, 29 Apr 2004 09:29:48 -0700, "Joshua Zucker" wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> Sometimes I get the error message
> "reference to an identifier before its definition: [name of function]"
> and PLT (at intermediate student level) seems to require that the function
> is defined before it is used.
> 
> But if we want two mutually recursive functions, obviously that won't work.
> And also the error message only seems to come up SOMETIMES when we do
> this, not consistently.
> 
> What am I not understanding about this?
> 
> Thanks,
> --Joshua Zucker


Posted on the users mailing list.