MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: message-driven function: more explanation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95642] Re: message-driven function: more explanation
  • From: dh <dh at metrohm.com>
  • Date: Fri, 23 Jan 2009 05:09:22 -0500 (EST)
  • References: <gl9n6q$aus$1@smc.vnet.net>


Hi Bert,

nothing exotic here. The return value is a function that consits of a 

Switch statement:



changeCounter$448[msg$_String:++]:=Switch[msg$,

"++",counter$448=counter$448+1;counter$448,

"--",counter$448=counter$448-1;counter$448,

"value",counter$448,

"reset",counter$448=100;counter$448,

_,Print[Unknown message to counter:,msg$];counter$448]



As you see, the Switch simply compares the strings.

hope this helps, Daniel







Bert Aerts (rm X) wrote:

> In the book Programming Paradigms with Mathematica 

> http://library.wolfram.com/infocenter/MathSource/1847/

> in the notebook Hw21.nb there is following function:

> 

> Clear[makeCounter];

> makeCounter::usage="makeCounter[] returns a message-driven function

> which manipulates a \"hidden\" counter variable.  The initial value

> of the counter variable may be set via an optional argument to

> makeCounter (default is 0).  Valid messages for the function are

> \"++\" (increment), \"--\" (decrement), \"value\", and \"reset\".

> For example, ctr = makeCounter[100]; ctr[\"value\"] returns 100.";

> 

> makeCounter[

>    defaultValue_Integer:0

> ] :=

>    Module[

>      { counter = defaultValue,

>        changeCounter

>      },

>      changeCounter[msg_String:"++"] :=

>        Switch[msg,

>          "++",

>            counter = counter + 1; counter,

>          "--",

>            counter = counter - 1; counter,

>          "value",

>            counter,

>          "reset",

>            counter = defaultValue; counter,

>          _,

>            Print["Unknown message to counter:", msg];

>            counter

>        ];

>      changeCounter

>    ];

> 

> ctr = makeCounter[100]

> ctr["value"]

> ctr["++"]

> 

> gives as output

> 

> changeCounter$723

> 100

> 101

> 

> Where can I find more explanation of this type of "message-driven function"?

> 




  • Prev by Date: Re: bug in LinearProgramming
  • Next by Date: Re: Problem with an integral
  • Previous by thread: message-driven function: more explanation
  • Next by thread: Re: message-driven function: more explanation