[plt-scheme] Puzzled by procedure ordering.

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Tue Oct 21 08:33:14 EDT 2008

It's ok to write a forward reference, so long as it doesn't actually
run until the variable has been defined.  When you refer to genproc in
work, you aren't actually running it yet, so there's no problem.  Once
you define genproc, all the references are defined and it's safe to
run either procedure.

When you move work below btok, the reference to work in [callback
work] gets run before the definition of work.  Now you have a problem;
the callback you're passing to btok hasn't been defined yet.

In general, you can define procedures in any order you like, but you
can't (safely) call a procedure until all its dependencies have been
defined.  I hope this helps clarify the issue.

On Tue, Oct 21, 2008 at 5:38 AM, Ferreira Maurizio
<maurizio.ferreira at seling.it> wrote:
> In the following example, the procedure "Work" has no problem
> in 'seeing the procedure "genproc".
> However if I move the procedure in the indicated place,
> Drscheme complains with the following error:
> reference to an identifier before its definition: work
> Any explanation ?
> Regards
> Maurizio
> #lang scheme/gui
> (define (work x y)
>  (let ((fun (genproc 3)))
>    (display (fun 5))))
> (define (genproc n)
>   (lambda (x) (+ x n)))
> (define frame (new frame% [label "test"]))
> (define btok (new button% [parent frame] [label "ok"] [callback work]))
> ;; ---- move work here and get an error.
> (send frame show #t)

Carl Eastlund

Posted on the users mailing list.