functions with states

Adrian Hey ahey@iee.org
Wed, 3 May 2000 05:20:38 +0100 (BST)


On Tue 02 May, Valery A.Khamenya wrote:
> My question concerns with "refined" side effect. In C we can use static
> variables in such a way:
> 
> int foo()
> {
>     static int foo_counter =0;
>     foo_counter++;
>     /* ... */
>     return foo_counter;
> }
> 
> what should I do for such a counting of function calls in Clean ?

Unfortunately, you can't do this sort of thing in a purely functional
language because you'd you'd lose all the nice mathematical (=safe
transformation) properties of your program. Any 'function' which
behaved the way you describe wouldn't really be a function at all.

Glasgow Haskell provides unsafePerformIO. You could probably use
this in conjunction with mutable variables to implement something like
your C code, but if you did you wouldn't have a purely functional program
(hence the name..unsafePerformIO). I don't think Clean has anything similar.

Regards
-- 
Adrian Hey