caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "sejourne kevin" <sejourne_kevin@yahoo.fr>
To: Brian Hurt <bhurt@spnz.org>, OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] OCaml compared as a scripting language
Date: Wed, 16 Jun 2004 13:00:25 +0200 (CEST)	[thread overview]
Message-ID: <20040616110025.23891.qmail@web41907.mail.yahoo.com> (raw)
In-Reply-To: <Pine.LNX.4.44.0406151359560.4243-100000@localhost.localdomain>

--- Brian Hurt <bhurt@spnz.org> 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


  parent reply	other threads:[~2004-06-16 11:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-15 18:27 Hellflame
2004-06-15 20:07 ` Brian Hurt
2004-06-16  2:26   ` skaller
2004-06-16 11:00   ` sejourne kevin [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-06-14  9:52 Richard Jones
2004-06-14 15:55 ` Brian Hurt
2004-06-14 16:29   ` Richard Jones
2004-06-15  6:40     ` Florian Hars
2004-06-15 16:13       ` Bruno.Verlyck
2004-06-15 17:15         ` Richard Jones
2004-06-15 17:35           ` John Goerzen
2004-06-15 18:16             ` Karl Zilles
2004-06-15 19:23               ` John Goerzen
2004-06-15 21:17                 ` Alex Baretta
2004-06-16  2:12             ` skaller
2004-06-15 17:41           ` Jon Harrop
2004-06-15 17:42           ` William D. Neumann

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=20040616110025.23891.qmail@web41907.mail.yahoo.com \
    --to=sejourne_kevin@yahoo.fr \
    --cc=bhurt@spnz.org \
    --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;
as well as URLs for NNTP newsgroup(s).