From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id EF5F2BB81 for ; Wed, 15 Dec 2004 04:20:42 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id iBF3KgF8032624 for ; Wed, 15 Dec 2004 04:20:42 +0100 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 EAA17858 for ; Wed, 15 Dec 2004 04:20:42 +0100 (MET) Received: from covert.brown-ring.iadfw.net (redirect4.airmail.net [209.196.123.153]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iBF3KeF6019592 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 15 Dec 2004 04:20:41 +0100 Received: from host-216-39-197-207.mylinuxisp.com ([216.39.197.207] helo=welgehausen.net) by redirect4.airmail.net with smtp (Exim 4.24) id 1CePjd-000B1o-Eq for caml-list@inria.fr; Tue, 14 Dec 2004 21:21:53 -0600 Date: Tue, 14 Dec 2004 21:20:13 -0600 From: Kurt Welgehausen To: caml-list@inria.fr Subject: Re: [Caml-list] Simple clock Message-ID: <41BFAD6D.mail1UT21XGTC@kwel.net> References: <20041214171716.GA10373@gentoo.malaquias.no-ip.org> <4d029f7e04121410172361f47a@mail.gmail.com> <20041214203930.GA26559@gentoo.malaquias.no-ip.org> In-Reply-To: <20041214203930.GA26559@gentoo.malaquias.no-ip.org> User-Agent: nail 10.4 1/19/03 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 41BFAD8A.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 41BFAD88.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 ocamldoc:01 ocamlopt:01 cmxa:01 char:01 char:01 printf:01 printf:01 stdout:01 ocaml:01 rec:01 gettimeofday:01 unix:01 unix:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.0 X-Spam-Level: (*** Here are a few things to investigate if you're learning ocaml *) (*** "**" in opening comment, for ocamldoc *) (** simple-clock.ml Repeatedly prints the current time at the standard output, at intervals of 1 sec How to compile: ocamlopt unix.cmxa simple-clock.ml -o simple-clock *) (*** You don't need to open the module if you qualify names with "Unix." *) (*open Unix*) (*** It's not important in such a simple program, but you can hide functions like print_tm that are used only by one other function *) (*let print_tm tm = print_int tm.tm_hour; print_char ':'; print_int tm.tm_min; print_char ':'; print_int tm.tm_sec *) let simple_clock () = let print_tm tm = (*** so you don't get "7:8:9" *) Printf.printf "%2d:%02d:%02d\n" tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec; flush stdout in (*** This works, but in ocaml you can do it in a more functional style *) (* while true do print_tm (Unix.localtime (Unix.time ())); print_newline (); ignore (Unix.select [] [] [] 1.0) done *) let rec loop () = print_tm (Unix.localtime (Unix.time ())); ignore (Unix.select [] [] [] (1. -. (fst (modf (Unix.gettimeofday ()))))); loop () in loop () in simple_clock () (*let _ = simple_clock ()*) (*** It's easier to read if you remove my comments and the code that I've commented out. Have fun *)