Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Christophe TROESTLER <Christophe.Troestler@umh.ac.be>
To: "O'Caml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: '_a
Date: Fri, 28 Jan 2005 14:42:18 +0100 (CET)	[thread overview]
Message-ID: <20050128.144218.80110994.Christophe.Troestler@umh.ac.be> (raw)
In-Reply-To: <1106873838.12114.128.camel@pelican.wigram>

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


  parent reply	other threads:[~2005-01-28 13:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-27  0:19 '_a Mike Hamburg
2005-01-27  0:51 ` [Caml-list] '_a Jacques Garrigue
2005-01-27  9:34   ` skaller
2005-01-27 10:02     ` Alex Baretta
2005-01-27 14:13     ` '_a Vincenzo Ciancia
2005-01-27 19:39       ` [Caml-list] '_a Jacques Carette
2005-01-28  0:57       ` skaller
2005-01-28 13:25         ` '_a Stefan Monnier
2005-01-28 14:46           ` [Caml-list] '_a skaller
2005-01-28 14:46           ` Keith Wansbrough
2005-01-28 15:48             ` skaller
2005-01-29  1:37               ` Michael Walter
2005-01-28 13:42         ` Christophe TROESTLER [this message]
2005-01-28 14:50           ` skaller
2005-01-28 12:54       ` Richard Jones
2005-01-28 14:39         ` Alex Baretta
2005-01-29  0:33   ` [Caml-list] '_a Dave Berry
2005-02-02  9:17     ` Jacques Garrigue
2005-02-03  7:41   ` Florian Hars

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050128.144218.80110994.Christophe.Troestler@umh.ac.be \
    --to=christophe.troestler@umh.ac.be \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox