From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA09434 for caml-red; Fri, 4 Aug 2000 19:57:07 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA23369 for ; Thu, 3 Aug 2000 21:20:26 +0200 (MET DST) Received: from animal.cs.chalmers.se (animal.cs.chalmers.se [129.16.225.30]) by nez-perce.inria.fr (8.10.0/8.10.0) with ESMTP id e73JKQH01463 for ; Thu, 3 Aug 2000 21:20:26 +0200 (MET DST) Received: from muppet70.cs.chalmers.se (muppet70.cs.chalmers.se [129.16.226.211]) by animal.cs.chalmers.se (8.8.5/8.8.5) with ESMTP id VAA25670 for ; Thu, 3 Aug 2000 21:20:24 +0200 (MET DST) Received: from localhost (taha@localhost) by muppet70.cs.chalmers.se (8.8.5/8.8.5) with ESMTP id VAA10760 for ; Thu, 3 Aug 2000 21:20:25 +0200 (MET DST) X-Authentication-Warning: muppet70.cs.chalmers.se: taha owned process doing -bs Date: Thu, 3 Aug 2000 21:20:25 +0200 (MET DST) From: Walid Taha To: caml-list@inria.fr Subject: Imperative programming in Caml In-Reply-To: <20000728115216.13749@pauillac.inria.fr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: weis@pauillac.inria.fr [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, Walid. --- 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";;