From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA10067; Wed, 16 Jun 2004 13:00:32 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 NAA12125 for ; Wed, 16 Jun 2004 13:00:31 +0200 (MET DST) Received: from web41907.mail.yahoo.com (web41907.mail.yahoo.com [66.218.93.158]) by concorde.inria.fr (8.12.10/8.12.10) with SMTP id i5GB0TSH023136 for ; Wed, 16 Jun 2004 13:00:30 +0200 Message-ID: <20040616110025.23891.qmail@web41907.mail.yahoo.com> Received: from [80.11.131.220] by web41907.mail.yahoo.com via HTTP; Wed, 16 Jun 2004 13:00:25 CEST Date: Wed, 16 Jun 2004 13:00:25 +0200 (CEST) From: =?iso-8859-1?q?sejourne=20kevin?= Subject: Re: [Caml-list] OCaml compared as a scripting language To: Brian Hurt , OCaml In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Miltered: at concorde with ID 40D0284D.007 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 hacks:01 stdin:01 get's:01 iterator:01 libs:01 cin:99 cin:99 stdin:01 libs:01 ecrit:01 ocaml:01 ocaml:01 int:01 int:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --- Brian Hurt a écrit : > On Tue, 15 Jun 2004, Hellflame wrote: > An example of this is implicit sideeffects > side-effects- something Perl > does a lot of. This is actually an efficiency in > the small, and it lets > you write quick hacks like (if I recall my Perl > correctly): > while (<>) { > $count += $_; > } > > The equivelent Ocaml code might look something like: > > let sumfile = > let rec loop cnt = > let line, eof = > try > (input_line stdin), false > with > | End_of_file -> "", true > in > if eof then > cnt > else > loop (cnt + (int_of_string line)) > in > loop 0 > ;; > > I just quintupled the lines of code there- 3 lines > to 15 lines. > > But the problem is that Perl get's in brevity (in > part) by sideeffects- it > sets the (effectively) global and widely used $_ > variable. If the code > got modified to: > while (<>) { > do_something(1, 2, 3); > $count += $_; > } files can be see as a kind of data for an iterator. In my personnals libs I have function like : let fold_lefti f x a = let r = ref x in for i = 0 to Array.length a - 1 do r := f i !r a.(i) done; !r ;; let file_fold_left fonction result file = let r = ref result in try while true do r:= fonction !r file done; !r with | End_of_file -> !r ;; ... For the sum I use the second: let sumfile name = let cin = open_in name in let result = file_fold_left (fun r f->r+int_of_string(input_line f)) 0 cin in close_in cin; result ;; and for stdin : let sumstdin = file_fold_left (fun r _->r+(int_of_string (read_line()) ) ) 0 () ;; So if I use my libs ( :-) ) then for the sum in write only 3 lines too. kevin Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout ! Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/ Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !Téléchargez Yahoo! Messenger sur http://fr.messenger.yahoo.com ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners