From: yoann padioleau <padator@wanadoo.fr>
To: Jonathan Roewen <jonathan.roewen@gmail.com>,
Troy Spier <computerfreakk48@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Hello
Date: Tue, 14 Mar 2006 09:57:09 +0100 (CET) [thread overview]
Message-ID: <32532497.1142326629119.JavaMail.www@wwinf1627> (raw)
>
> > 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.
next reply other threads:[~2006-03-14 8:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-14 8:57 yoann padioleau [this message]
-- strict thread matches above, loose matches on Subject: below --
2006-03-14 1:21 Hello Troy Spier
2006-03-14 1:26 ` [Caml-list] Hello Jonathan Roewen
2006-03-14 1:29 ` Jonathan Roewen
2006-03-14 1:35 ` Corey O'Connor
2004-01-28 10:53 bgadil
2004-01-28 10:01 varesenews
2004-01-27 10:36 bessam.abdulrazak
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=32532497.1142326629119.JavaMail.www@wwinf1627 \
--to=padator@wanadoo.fr \
--cc=caml-list@inria.fr \
--cc=computerfreakk48@yahoo.com \
--cc=jonathan.roewen@gmail.com \
/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