Re: "ocaml_beginners":: alternating interactive and compiled modes
- --- In ocaml_beginners@y..., Stalkern 2 <stalkern2@t...> wrote:
> I agree on everything, but there is a strong reason for "globalrefs" in
> ocaml; in a functional-featured language, where lambda calculustakes charge
> gently of a number of states, not only refs are strictly speaking auseful.
> nonfunctional element, but using refs for *local* scopes is seldom
In italian I will say "se c'e' fantasia, non c'e' limite alle porcate
che ci si puo' inventare", that is, you can find an application
for everything, even for the more unuseful things: yes, the main use
for the ref is as globals, even in my code, but sometimes I use
them to store local states too, anyway, as you say, just a question
An example of local ref can be the following: the idea, for
C-coders, is to have a static variable in a function, that is
a variable whose scope is local to a function, but whose
value is keeped from call to call:
let myFunction =
let lastX = ref 0.0 in
(fun x ->
(* blablablathatisdosomething; *)
Printf.printf "LastX=%f, currentX=%f\n" !lastX x;
lastX := x;
val myFunction : float -> unit = <fun>
In this way, I have a local variable, which can be seen
just in myFunction, but whose life is extended to the
all program life.
By the way, I agree too, that everything is a question of style:
one could implement such a thing with in continuation passing style
with no refs.
> of programming styles, I believe. I often consider Ocaml as animperative
> environment with functional functions... it should be time forstudying OO!!
> ;-))What!?!? you are completely wrong! Ocaml is a functional
language with imperative extensions!!!! :-)
And by the way, I wrote some small application in OO (using C),
but I don't like this style at all: you can always model
your ideas as if everything were an object, but sometime
such a paradigma is good for your application, some others
no: the same when you want to write a completley (pure)
functional program, or a completely continuation passing
style program: in some occcasion the model makes your
programming much easier, sometimes make it much harder.
Models are a good thing, until you adapt the models
to reality and not reality to models.