s c h e m a t i c s : c o o k b o o k

/ Cookbook.IdiomSharingState

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Sharing state between two functions

Problem

Defining two functions sharing state without polluting the top level with unneccesary definitions.

Solution

There are several solutions to this problem.

1. Using define-values

 (define-values (inc dec reset)
      (let ([state 0])
        (define (inc)  (set! state (+ state 1)) state)
        (define (dec)  (set! state (- state 1)) state)
        (define (reset)(set! state 0)           state)

        (values inc dec reset))) 

2. Using modules

  (module foo mzscheme
      (provide inc dec reset)

      (define state 0)
      (define (inc)  (set! state (+ state 1)) state)
      (define (dec)  (set! state (- state 1)) state)
      (define (reset)(set! state 0)           state)) 

3. Using a let to keep the shared state out of the top level

  (define inc   'uninitialized)
  (define dec   'uninitialized)
  (define reset 'uninitialized)
  (let ([state 0])
    (define (internal-inc)    (set! state (+ state 1)) state)
    (define (internal-dec)    (set! state (- state 1)) state)
    (define (internal-reset)  (set! state 0)           state)

    (set! inc   internal-inc)
    (set! dec   internal-inc)
    (set! reset internal-reset)) 

Discussion

The first and third solutions uses nothing more than plain R5RS (define-values can be defined using syntax-rules).

The second solution is perhaps the most common, when there is no compatibility concerns.

Contributors

-- JensAxelSoegaard

Comments

Sharing state between two functions - aside from R5RS compliance, what are the relative advantages/disadvantages? How does each solution work? How will each solution affect usage?

-- GordonWeakliem - 26 Apr 2004

Solution 1 shows intent most clearly. Solution 2 is often used if you already are using modules (no extra work) Solution 3 is the only directly portable.

-- JensAxelSoegaard

Is there an implementation of define-values available anywhere?

-- AndrewWilcox - 05 Dec 2004

In some Scheme implementations such as PLT Scheme? it is simply a primitive. In others one can by simple means define your own. As an example the following was written by Shiro Kawai for Gauche?:

(define-syntax define-values
  (syntax-rules ()
    ((_ "gentmp" (tmp ...) () (var ...) expr)
     (begin (define var (undefined)) ...
            (receive (tmp ...) expr
              (set! var tmp) ...
              (undefined))))
    ((_ "gentmp" (tmp ...) (v v2 ...) (var ...) expr)
     (define-values "gentmp" (tmp ... tmp1) (v2 ...) (var ...) expr))
    ((_ (var  ...) expr)
     (define-values "gentmp" () (var ...) (var ...) expr))
    ((_ . else)
     (syntax-error "malformed define-values" (define-values . else)))
    ))
It uses receive , which is defined in it's own SRFI. It also uses (undefined) to produce an undefined value. One can use (if #f 42) in stead.

See gauche-init.scm.

-- JensAxelSoegaard - 05 Dec 2004

Thank you.

I notice that in R5RS internal definitions (R5RS 5.2.2) and in module systems such as in Chez Scheme, a series of definitions may be followed by expressions, but further definitions can't follow an expression. Since this define-values expands into a receive expression, a define-values defintion can not be followed by more definitions within a body or a module. I wonder if it is possible to implement a define-values macro in a way so that it can be followed by more definitions. I've been looking at this, and I'm beginning to suspect not.

-- AndrewWilcox - 05 Dec 2004

Interesting question. I suspect that is the reason define-values is a primitive in PLT Scheme?, but try asking the same question at comp.lang.scheme?.

-- JensAxelSoegaard - 05 Dec 2004

CookbookForm
TopicType: Recipe
ParentTopic: IdiomRecipes
TopicOrder:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest