From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id B67EEBBBB for ; Tue, 14 Mar 2006 09:57:10 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k2E8v90Y013040 for ; Tue, 14 Mar 2006 09:57:10 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA23007 for ; Tue, 14 Mar 2006 09:57:09 +0100 (MET) Received: from smtp16.wanadoo.fr (smtp16.wanadoo.fr [193.252.23.89]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k2E8v97A028358 for ; Tue, 14 Mar 2006 09:57:09 +0100 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf1604.wanadoo.fr (SMTP Server) with ESMTP id 2BD12700008C for ; Tue, 14 Mar 2006 09:57:09 +0100 (CET) Received: from wwinf1627 (wwinf1627 [172.22.147.101]) by mwinf1604.wanadoo.fr (SMTP Server) with ESMTP id 209D97000081; Tue, 14 Mar 2006 09:57:09 +0100 (CET) X-ME-UUID: 20060314085709133.209D97000081@mwinf1604.wanadoo.fr Message-ID: <32532497.1142326629119.JavaMail.www@wwinf1627> From: yoann padioleau Reply-To: padator@wanadoo.fr To: Jonathan Roewen , Troy Spier Subject: Re: [Caml-list] Hello Cc: caml-list@inria.fr Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [193.54.76.52] X-Wum-Nature: EMAIL-NATURE X-WUM-FROM: |~| X-WUM-TO: |~||~| X-WUM-CC: |~| X-WUM-REPLYTO: |~| Date: Tue, 14 Mar 2006 09:57:09 +0100 (CET) X-Miltered: at nez-perce with ID 44168566.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 44168565.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; printf:01 printf:01 higher-order:01 caml-list:01 exception:01 functions:01 lisp:01 lisp:01 defined:02 output:02 output:02 string:02 string:02 raise:03 let:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 > > > print_string "What's your name ? "; let name = read_line () in Printf.printf > > "Hello, %s.\n" name ;; > > let o = open_out "filename";; > output_string o name;; > close_out o;; A better (advanced) way is to do it the Lisp way: with_open_outfile "filename" (fun chan -> output_string chan name ); where with_open_outfile is defined by: let with_open_outfile filename f = let chan = open_out filename in let res = f chan in begin close_out chan; res end Everywhere where 2 functions must be balanced (here open and close), it is cleaner to introduce a single higher-order function to enforce such a protocol. You can also use a function unwind_protect (again invented by the Lisp folk I think) to also automagically close the channel when f raise an exception.