Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] productivity improvement
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] productivity improvement
John Max Skaller wrote:
> 
> As someone said recently "ocaml forever" :-))
> 

Just today I have found a counterexample where C does a 
better job than O'Caml--at least within the context of my 
understanding of and ability with the two languages.

This is the problem: I have a text file containing a fairly 
long table extracted from a database (approximately 73000 
lines). I had to write a program to parse each line, extract 
only the relevant fields and reformat dates and times 
according to some sensible format (such as dd/mm/yyyy 
hh:mm:ss as opposed to yyyymmdd hhmmss00). This is how I 
solved the problem in C:


#include <stdio.h>

int main ( int argc , char *argv[]) {

#define ALF " %*s"
#define ISIN " %*s"
#define DATE " %4s%2s%2s"
#define TIME " %2s%2s%2s%*2s"
#define PRICE "%s"
#define QN     "%s"
#define CNTVL  "%*s"


   char scanner[] = ALF ISIN DATE TIME PRICE QN CNTVL;
   char printer[] = "%s/%s/%s\t%s:%s:%s\t%s\t%s\n";

   char year[8], month[8], day[8];
   char hh[4], mm[4], ss[4];
   char price[64], qn[64];

   int n;

   while (1) {
     n=scanf(scanner, year, month, day,
	hh, mm, ss, price, qn);
     if (n==EOF) break;
     printf(printer, day, month, year,
	hh, mm, ss, price, qn);
   }
}


I was not able to figure out an easy way to do this in 
O'Caml. Of course, I could have used ocamllex and ocamlyacc 
to define a lexer and a parser, but what I really needed was 
a scanf function.

My intuition is that we badly need a *functional* scanf. Of 
course, I despise the idea of a format string. I had to use 
7 #defines in order to make some sense of the scanf format, 
and I might have done the same for printf, where the format 
not a little simpler.

Of course, my understanding of O'Caml is incomplete, and I'm 
sure someone will be good enough to teach how I could have 
done the same in O'Caml with only a fraction of the lines of 
code.

Alex

-------------------
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