functions with states

Valery A.Khamenya khamenya@module.ru
Wed, 3 May 2000 10:47:23 +0400


> 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.

I feel Clean has such a possibility. Indeed, let us consider some pure
function

g::Int ->Int
g math_arg = ...

then we should make closure for g, something like this:

foo :: *Int Int->(*Int,Int)
foo call_counter math_arg = (call_counter+1, g math_arg)

expression  "((snd o foo) call_counter)" looks like our pure function 'g',
but we have some ability to keep our side effects in call_counter. Those
side effects should be refined in functional style and all is OK.

But this is my ill theory. What will say Clean guru?


P.S. Is there unique type "functional"?

P.S.S. A year ago I said my friends about my strange feeling with Clean.
Programming on Clean is like writing poems!             1. Sometime I hardly
impement in Clean even easy 2-lines C-code! (like function in this letter)
        2. For almost every my implementation there is elegant case two
times shorter and two times quicker!
        And Clean compiler like censor... :))