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-04 (17:57)
From: Walid Taha <taha@c...>
Subject: Imperative programming in Caml

[Apologies in advance for purists that this project might offend.]

Dear all,

Below is one of my first attempts at imperative programming in ML: a
program that reads a list of numbers and squares them, using a "mutable
list".  The presence of a "while" construct and easy of terminal IO in
Caml should help an imperative programmer feel at home.  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.  If any one has suggestions
for making this program more "imperative", I would appreciate it.

Many thanks in advance,



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;
         else begin
 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 " ";
 print_string "\n\nGood bye!\n\n";;