Version française
Home     About     Download     Resources     Contact us    
Browse thread
'_a
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Re: '_a
On Sat, 2005-01-29 at 00:42, Christophe TROESTLER wrote:
> On 28 Jan 2005, skaller <skaller@users.sourceforge.net> wrote:
> > 
> > let x = 
> > 	let rec f l -> | [] -> raise Not_found 
> > 	| h :: t -> if h == v then (raise Found; 0) else f t
> > 	in try f l with Found -> 1 | Not_found -> 2
> > in print_endline (string_of_int x)
> > 
> > where the compiler doesn't know f l cannot return, so it needs a
> > useless '0' after the Found case to get the typing correct.
> 
> Not quite,

[]

> Maybe you mean something like
> 
> let cl file =
>   let fh = open_in file in
>   let nl = ref 0 in
>   try
>     while true do
>       let _ = input_line fh in
>       incr nl
>     done
>   with End_of_file -> !nl

Thanks for a better example (well a right one has to better :)

> where the [while] has type [unit] while [!nl] has type [int] and the
> two cannot be unified.  But this is because there is no way for the
> compiler to know that a loop indeed never ends, so one has to tell:
> 
> let cl file =
>   let fh = open_in file in
>   let nl = ref 0 in
>   try
>     while true do
>       let _ = input_line fh in
>       incr nl
>     done;
>     assert false
>   with End_of_file -> !nl
> 
> With that everything is fine.

Yes, that's a better solution too.

-- 
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