English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
overhead of GC in caml runtime?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-08-05 (18:13)
From: Markus Mottl <mottl@m...>
Subject: Re: Imperative programming in Caml
On Thu, 03 Aug 2000, Walid Taha wrote:
> [Apologies in advance for purists that this project might offend.]

It is not about being "purist" - just about solving problems effectively...

> The presence of a "while" construct and easy of terminal IO in Caml
> should help an imperative programmer feel at home.

I agree that "impurity" can come handy at times (but very seldom!).

> But I am concerned (and a bit surprised, actually) that the use of "let"
> bindings and the presence of normal variables in addition to "mutable"
> variables might make it more difficult to explain this program to a
> beginer that is *not* interested in the functional aspects.

It is a pity that he is not interested in the functional aspects (why?)!
If I wanted to confuse a beginner with a piece of code, I'd take one that
is relatively long, does not use many abstractions and ... is imperative.

Yours (excuse my criticism, I hope you understand the positive intention)
comes quite close to working as a repellent for beginners. - Even I have
troubles understanding what problem the code tries to solve!

> If any one has suggestions for making this program more "imperative", I
> would appreciate it.

If you absolutely need to make it imperative, please introduce some
additional functions (procedures) with intuitiv names so that the meaning
is clear.

In any case, the reason for using functional programming is not to annoy
beginners: in fact, since functional programming is on a somewhat higher
level compared to imperative programming, it is easier to reason about
"what" to solve rather than "how", which is what a beginner should be
concerned about. If he (she?) is already "biased" towards imperative
programming, I'd rather ask for hints on how to explain the merits of
functional style to motivate the beginner to try it.

Just compare the functional version with the imperative one (see below): I
can impossibly imagine that anybody would rather feel attracted by the much
more difficult imperative solution. I hope you'll succeed in "seducing"
your beginner to the elegance of functional style!

Markus Mottl

  let rec read_ints () =
    print_string "Enter a number: ";
    let number = read_int () in
    if number <> 0 then number :: read_ints ()
    else []

  let squareMany () =
    print_string "\nPlease enter zero (0) to stop.\n\n";
    let lst = read_ints () in
    print_string "Here are the squares of the numbers you entered: ";
    List.iter (fun n -> print_int (n*n); print_char ' ') lst;
    print_string "\n\nGood bye!\n\n"

> let squareMany () =
>  print_string "\nPlease enter zero (0) to stop.\n\n"; 
>  let finished = ref false 
>  and list = ref Empty in 
>  let here = ref list in
>  while not(!finished) do
>         print_string "Enter a number : ";
>         let number = read_int () in
>         if number<>0 
>          then begin
>                let new = ref Empty in
>                !here := Cell (number, new);
>                here := new;
>               end
>          else begin
>                finished:=true;
>               end 
>        done;
>  print_string "Here are the squares of the numbers you entered: ";
>  while (!list)<>Empty do
>        let (Cell(number, rest)) = !list in
>            print_int (number*number);
>            list := !rest;
>            print_string " ";
>        done;
>  print_string "\n\nGood bye!\n\n";;

Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl