functions with states

Valery A.Khamenya khamenya@module.ru
Wed, 3 May 2000 18:43:11 +0400


Hello Corrin and clean-list.

> Hello, I'm sending this to ydirectly because for some reason the the clean
> list bounced.

I can add here that the web interface from Clean Web site to Clean mailing
list still gives an access to letters no later than year 1999.

> >> [Static in C]
>
> >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.
> >
> >[Haskell solution] I don't think Clean has anything similar.
>
> Actually Clean does, but you should make sure you understand Adrian's
reply
> or you're code won't work.

I hope I understand what the 'side effects' are and what the pure functional
languages give us.

> The mathematical properties of functional
> languages enable the compiler to do some very clever things.  A C
> programmer would be very surprised to see the order in which Clean
executes
> the functions you type and if your static variable is dependant on calling
> order you're going to get the wrong results.

I am ready for such surprises from language developed for parallel
computation... More than that, existence of such a surprises is a good
criterion for me: the language might be ripe for parallel
computation.

> The mathematically correct way is to have a variable, perhaps called state
> (or even world :) that contains your static integer.  The state variable
is
> then another argument to your function and the function returns the new
> state as well as whatever it returned before.

yes, I keep those principles in mind, but I can't _implement_ in Clean the
functions like this.

> This solution isn't great and often leads to the introduction of bugs,

?!

> but
> most of the time you can redesign your function so it doesn't need a
static
> variable anyway.  Perhaps if you said what you were trying to do we could
> suggest an alternative approach?

I have 10 years experience of programming in nonfunctional languages.
In traditional languages the side effects are the main facility for having
'ultimate aim' of the program-- such a 'ultimate side effect'.

I'd like to learn the 'side effect'-less way of programing. That is why I'd
like to know how to implement usual C-code in Clean. The open question
for me is: "is Clean ripe enough?"

[of course Charlie game is a very good practice criterion but not sufficient
for me. Indeed, you can code such a game in assemler but the assembler
is not a good choise for every task. ]

> If you really do need to store state, the way I do it is by using Clean's
C
> calling ability and storing values inside my C functions.  I have used
this
> to implement SQL database access and memo.  Be aware though that both of
> these applications are _conceptually_ stateless and my solution would not
> work if they weren't.
> Have a look at the h_to_clean library if you want to
> use this approach.

certainly! but this will concern with other significant issue for me:
"interaction with general high level languages e.g. C/C++"

However the Clean already is very attractive and nice.

thank you dear Corrin,

best regards,
Valery A.Khamenya