Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: Walid Taha <taha@cs.chalmers.se>
Cc: caml-list@inria.fr
Subject: Re: Imperative programming in Caml
Date: Fri, 4 Aug 2000 21:43:24 +0200	[thread overview]
Message-ID: <20000804214324.A17674@miss.wu-wien.ac.at> (raw)
In-Reply-To: <Pine.SOL.4.21.0008032108470.8979-100000@muppet70.cs.chalmers.se>; from taha@cs.chalmers.se on Thu, Aug 03, 2000 at 21:20:25 +0200

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



  reply	other threads:[~2000-08-05 18:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-25 21:47 overhead of GC in caml runtime? Norman Ramsey
2000-07-28  9:52 ` Xavier Leroy
2000-08-03 19:20   ` Imperative programming in Caml Walid Taha
2000-08-04 19:43     ` Markus Mottl [this message]
2000-08-04 19:57       ` Walid Taha
2000-08-06  1:59         ` John Prevost
2000-08-08 18:01           ` Walid Taha
2000-08-08 18:23             ` John Prevost
2000-08-08 18:30               ` Walid Taha
2000-08-08 21:10                 ` Pierre Weis
2000-08-09 13:50                   ` Walid Taha
2000-08-04 18:33 Don Syme
2000-08-04 19:48 ` Walid Taha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20000804214324.A17674@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=caml-list@inria.fr \
    --cc=taha@cs.chalmers.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox