Browse thread
Simple clock
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Kurt Welgehausen <kwel@k...> |
| Subject: | Re: [Caml-list] Simple clock |
(*** 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 *)