[plt-scheme] Defining global functions with shared state?

From: Thomas Chust (chust at web.de)
Date: Tue Mar 17 16:25:51 EDT 2009

2009/3/17 Jeff de Vries <jdevries at pfrog.com>:
> I'm trying to create a macro that creates a set of global function
> definitions where those functions share some common state.  Basically, the
> intent is something like the following:
> ;;; doesn't work, creates local definitions instead of global ones
> (let ((state (create-common-state)))
>   (define (foo x) (use-common-state-one-way x state))
>   (define (bar x) (use-common-state-another-way x state)))
> [...]

Hello Jeff,

you could let your macro expand into a define-values form that looks like this:

  (define-values (foo bar)
    (let ([state (create-common-state)])
        (lambda (x) (use-common-state-one-way x state))
        (lambda (x) (use-common-state-another-way x state)))))


When C++ is your hammer, every problem looks like your thumb.

Posted on the users mailing list.