caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Simple clock
@ 2004-12-14 17:17 romildo
  2004-12-14 17:38 ` [Caml-list] " Keith Wansbrough
  0 siblings, 1 reply; 9+ messages in thread
From: romildo @ 2004-12-14 17:17 UTC (permalink / raw)
  To: caml-list

Hello.

I am willing to write a very very simple clock in
OCaml, but do not know exactly how to do it.

I would like to start with a console program
that just prints the current time every second.
The output would be something like the following:

$ simpleclock
15:15:58
15:15:59
15:16:00
15:16:01

I want to receive sugestions on how to implement
this.

Regards,

Romildo


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 17:17 Simple clock romildo
@ 2004-12-14 17:38 ` Keith Wansbrough
  2004-12-14 18:17   ` Lars Nilsson
  2004-12-14 20:43   ` romildo
  0 siblings, 2 replies; 9+ messages in thread
From: Keith Wansbrough @ 2004-12-14 17:38 UTC (permalink / raw)
  To: romildo; +Cc: caml-list

> 
> $ simpleclock
> 15:15:58
> 15:15:59
> 15:16:00
> 15:16:01
> 
> I want to receive sugestions on how to implement
> this.

Thread.delay
Unix.time
Unix.localtime

--KW 8-)
-- 
Keith Wansbrough <kw217@cl.cam.ac.uk>
http://www.cl.cam.ac.uk/users/kw217/
University of Cambridge Computer Laboratory.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 17:38 ` [Caml-list] " Keith Wansbrough
@ 2004-12-14 18:17   ` Lars Nilsson
  2004-12-14 20:39     ` romildo
  2004-12-14 20:43   ` romildo
  1 sibling, 1 reply; 9+ messages in thread
From: Lars Nilsson @ 2004-12-14 18:17 UTC (permalink / raw)
  To: caml-list

On Tue, 14 Dec 2004 17:38:50 +0000, Keith Wansbrough
<Keith.Wansbrough@cl.cam.ac.uk> wrote:
> Thread.delay
> Unix.time
> Unix.localtime

Or Unix.select instead of Thread.delay, for that tried-and-true
solution that will give you sub-second sleep capability.

  ignore(Unix.select [] [] [] 0.5)

Lars Nilsson


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 18:17   ` Lars Nilsson
@ 2004-12-14 20:39     ` romildo
  2004-12-14 21:52       ` Ville-Pertti Keinonen
  2004-12-15  3:20       ` Kurt Welgehausen
  0 siblings, 2 replies; 9+ messages in thread
From: romildo @ 2004-12-14 20:39 UTC (permalink / raw)
  To: Lars Nilsson; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 613 bytes --]

On Tue, Dec 14, 2004 at 01:17:01PM -0500, Lars Nilsson wrote:
> On Tue, 14 Dec 2004 17:38:50 +0000, Keith Wansbrough
> <Keith.Wansbrough@cl.cam.ac.uk> wrote:
> > Thread.delay
> > Unix.time
> > Unix.localtime
> 
> Or Unix.select instead of Thread.delay, for that tried-and-true
> solution that will give you sub-second sleep capability.
> 
>   ignore(Unix.select [] [] [] 0.5)

Here is my simple clock in Ocaml. Have I done it right?
Any comments?

Particularly, the while, print_tm and print_newline execution
time is not counted. Would that introduce an accurace in
my clock? If so, is that inevitable?

Romildo

[-- Attachment #2: simple-clock.ml --]
[-- Type: text/plain, Size: 496 bytes --]

(* 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
*)

open Unix

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 () =
  while true do
    print_tm (Unix.localtime (Unix.time ()));
    print_newline ();
    ignore (Unix.select [] [] [] 1.0)
  done

let _ = simple_clock ()

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 17:38 ` [Caml-list] " Keith Wansbrough
  2004-12-14 18:17   ` Lars Nilsson
@ 2004-12-14 20:43   ` romildo
  2004-12-15  3:00     ` skaller
  1 sibling, 1 reply; 9+ messages in thread
From: romildo @ 2004-12-14 20:43 UTC (permalink / raw)
  To: Keith Wansbrough; +Cc: romildo, caml-list

[-- Attachment #1: Type: text/plain, Size: 565 bytes --]

On Tue, Dec 14, 2004 at 05:38:50PM +0000, Keith Wansbrough wrote:
> > 
> > $ simpleclock
> > 15:15:58
> > 15:15:59
> > 15:16:00
> > 15:16:01
> > 
> > I want to receive sugestions on how to implement
> > this.
> 
> Thread.delay
> Unix.time
> Unix.localtime

I have tried the following program, but its
compilation fails with the message:

$ ocamlopt -thread threads.cmxa unix.cmxa simple-clock.ml -o simple-clock
No implementations provided for the following modules:
  Unix referenced from /usr/lib/ocaml/threads/threads.cmxa(Thread)

What does that mean?

Romildo

[-- Attachment #2: simple-clock.ml --]
[-- Type: text/plain, Size: 512 bytes --]

(* simple-clock.ml
   Repeatedly prints the current time at the standard output,
   at intervals of 1 sec

   How to compile:

   ocamlopt -thread threads.cmxa unix.cmxa simple-clock.ml -o simple-clock
*)

open Unix
open Thread

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 () =
  while true do
    print_tm (Unix.localtime (Unix.time ()));
    print_newline ();
    Thread.delay 1.0
  done

let _ = simple_clock ()

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 20:39     ` romildo
@ 2004-12-14 21:52       ` Ville-Pertti Keinonen
  2004-12-15  3:20       ` Kurt Welgehausen
  1 sibling, 0 replies; 9+ messages in thread
From: Ville-Pertti Keinonen @ 2004-12-14 21:52 UTC (permalink / raw)
  To: romildo; +Cc: Lars Nilsson, caml-list

On Tue, 2004-12-14 at 18:39 -0200, romildo@uber.com.br wrote:

> Particularly, the while, print_tm and print_newline execution
> time is not counted. Would that introduce an accurace in
> my clock? If so, is that inevitable?

It'll always print the correct time, at approximately one-second
intervals, but as you suspect, it'll drift over time.

What you probably want for the delay is something like:

ignore (Unix.select [] [] [] (1.0 -. (fst (modf (Unix.gettimeofday
())))))

Unix.gettimeofday () returns the time as a floating point number with
reasonable accuracy, modf splits it into fractional and integral parts,
fst selects the first one (the fractional part), so the select returns
at (or as close to as possible) the next integral second.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 20:43   ` romildo
@ 2004-12-15  3:00     ` skaller
  0 siblings, 0 replies; 9+ messages in thread
From: skaller @ 2004-12-15  3:00 UTC (permalink / raw)
  To: romildo; +Cc: Keith Wansbrough, caml-list

On Wed, 2004-12-15 at 07:43, romildo@uber.com.br wrote:

> $ ocamlopt -thread threads.cmxa unix.cmxa simple-clock.ml -o simple-clock
> No implementations provided for the following modules:
>   Unix referenced from /usr/lib/ocaml/threads/threads.cmxa(Thread)
> 
> What does that mean?

Try unix.cmxa before thread.cmxa

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-14 20:39     ` romildo
  2004-12-14 21:52       ` Ville-Pertti Keinonen
@ 2004-12-15  3:20       ` Kurt Welgehausen
  2004-12-15 17:37         ` Evan Martin
  1 sibling, 1 reply; 9+ messages in thread
From: Kurt Welgehausen @ 2004-12-15  3:20 UTC (permalink / raw)
  To: caml-list

(*** 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 *)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Simple clock
  2004-12-15  3:20       ` Kurt Welgehausen
@ 2004-12-15 17:37         ` Evan Martin
  0 siblings, 0 replies; 9+ messages in thread
From: Evan Martin @ 2004-12-15 17:37 UTC (permalink / raw)
  To: Kurt Welgehausen; +Cc: caml-list

On Tue, Dec 14, 2004 at 09:20:13PM -0600, Kurt Welgehausen wrote:
>     Printf.printf "%2d:%02d:%02d\n"
>                         tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec;
>     flush stdout in

Or just
Printf.printf "%2d:%02d:%02d\n%!"
              tm.Unix.tm_hour tm.Unix.tm_min tm.Unix.tm_sec

>From the docs on conversion characters:
  !: take no argument and flush the output.

-- 
Evan Martin
martine@danga.com
http://neugierig.org


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-12-15 17:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-14 17:17 Simple clock romildo
2004-12-14 17:38 ` [Caml-list] " Keith Wansbrough
2004-12-14 18:17   ` Lars Nilsson
2004-12-14 20:39     ` romildo
2004-12-14 21:52       ` Ville-Pertti Keinonen
2004-12-15  3:20       ` Kurt Welgehausen
2004-12-15 17:37         ` Evan Martin
2004-12-14 20:43   ` romildo
2004-12-15  3:00     ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).