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 UAA22750 for caml-red; Sat, 5 Aug 2000 20:14:18 +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 VAA12762 for ; Fri, 4 Aug 2000 21:57:54 +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 e74Jvrb20710 for ; Fri, 4 Aug 2000 21:57:53 +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 VAA24158; Fri, 4 Aug 2000 21:57:52 +0200 (MET DST) Received: from localhost (taha@localhost) by muppet70.cs.chalmers.se (8.8.5/8.8.5) with ESMTP id VAA29863; Fri, 4 Aug 2000 21:57:52 +0200 (MET DST) X-Authentication-Warning: muppet70.cs.chalmers.se: taha owned process doing -bs Date: Fri, 4 Aug 2000 21:57:52 +0200 (MET DST) From: Walid Taha To: Markus Mottl cc: caml-list@inria.fr Subject: Re: Imperative programming in Caml In-Reply-To: <20000804214324.A17674@miss.wu-wien.ac.at> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: weis@pauillac.inria.fr Dear Markus, Thanks for the nice rewrite, but it ain't purely functional still! :) Your point is well-taken. I *am* a functional programmer, though I haven't posted to this list before. I am interested in seeing how easy it is to explain imperative concepts in Caml. A bit to my surprise, it is turning out to be more subtle than I thought. In some cases, interesting remedies might exits. Your rewrite of the example is quite interesting and I have noted it, but it still uses IO, which is imperative. I'll summarize the discussions at some point and send them to the list. Best regards, Walid. On Fri, 4 Aug 2000, Markus Mottl wrote: > 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 >