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 UAA23540 for caml-red; Sat, 5 Aug 2000 20:13:19 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA07356 for ; Fri, 4 Aug 2000 21:43:23 +0200 (MET DST) Received: from miss.wu-wien.ac.at (miss.wu-wien.ac.at [137.208.107.17]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e74JhMX27280 for ; Fri, 4 Aug 2000 21:43:22 +0200 (MET DST) Received: (from mottl@localhost) by miss.wu-wien.ac.at (8.9.0/8.9.0) id VAA22448; Fri, 4 Aug 2000 21:43:24 +0200 (MET DST) Date: Fri, 4 Aug 2000 21:43:24 +0200 From: Markus Mottl To: Walid Taha Cc: caml-list@inria.fr Subject: Re: Imperative programming in Caml Message-ID: <20000804214324.A17674@miss.wu-wien.ac.at> References: <20000728115216.13749@pauillac.inria.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2i In-Reply-To: ; from taha@cs.chalmers.se on Thu, Aug 03, 2000 at 21:20:25 +0200 Sender: weis@pauillac.inria.fr 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! Regards, 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