Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Walid Taha <taha@cs.chalmers.se>
To: caml-list@inria.fr
Subject: Imperative programming in Caml
Date: Thu, 3 Aug 2000 21:20:25 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.SOL.4.21.0008032108470.8979-100000@muppet70.cs.chalmers.se> (raw)
In-Reply-To: <20000728115216.13749@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";;




  reply	other threads:[~2000-08-04 17:57 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   ` Walid Taha [this message]
2000-08-04 19:43     ` Imperative programming in Caml Markus Mottl
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=Pine.SOL.4.21.0008032108470.8979-100000@muppet70.cs.chalmers.se \
    --to=taha@cs.chalmers.se \
    --cc=caml-list@inria.fr \
    /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