[racket] Making a Racket function "recallable"

From: Stephen Bloch (bloch at adelphi.edu)
Date: Tue Feb 14 06:55:28 EST 2012

On Feb 13, 2012, at 11:52 PM, Joe Gilray wrote:

> This function works, but is not "recallable" (is there a better word?).  In other words I cannot do the following:
> 
> (define f1 fib)
> (define f2 fib)
> (f1) (f1) (f2) ...
> 
> and have f1 and f2 be separate fibonacci lists.

This is the impulse motivating the historical shift from imperative to object-oriented programming.  Whenever you have one or more functions that share mutable state, ask yourself whether one might ever conceivably want more than one instance of that state.  Look at your favorite 40-year-old Fortran, BASIC, or Pascal program: odds are that a bunch of different procedures all operate on the same global data.  If, for some reason, you needed two instances of all of this, you'd need to duplicate all the procedures and the global variables, which is a Royal Pain.  One solution is to have each procedure take the relevant data as parameters (by reference, so they can be mutated), and that works as long as there aren't too many different chunks.  I recall writing, as an undergraduate, an OS simulation in which every function took about fifteen parameters, because the teacher had decreed "no global variables".  In retrospect, I should have packaged them up into a record/struct, which would not only make the code cleaner and less error-prone but also make it easy to simulate multiple instances of the OS.

Stephen Bloch
sbloch at adelphi.edu



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120214/6f22332f/attachment.html>

Posted on the users mailing list.