Browse thread
'_a
[
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: | 2005-01-28 (13:42) |
From: | Christophe TROESTLER <Christophe.Troestler@u...> |
Subject: | Re: [Caml-list] Re: '_a |
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, let find v l = let x = let rec f = function | [] -> raise Not_found | h :: t -> if h = v then raise Found else f t in try f l with Found -> 1 | Not_found -> 2 in print_endline (string_of_int x) has type "val find : 'a -> 'a list -> unit = <fun>". (BTW, note that the equality is "=" in Caml.) 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 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. Regards, ChriS