[Haskell-cafe] #haskell works

Roman Leshchinskiy rl at cse.unsw.edu.au
Sat Dec 15 19:12:47 EST 2007


Andrew Coppin wrote:
> 
> Then Mr C++ looked at it and said "OMG! You don't *never* use strlen() 
> inside a loop!" and the second version was writting:
> 
> file[FILE_SIZE] = ' ';
> n = 0;
> maxLength = 0;
> while( n < FILE_SIZE )
> {
> while( file[n++] == ' ' ); wStart = n;
> while( file[n++] != ' ' ); wLength = n - wStart;
> if( wLength > maxLength )
> {
>   longestWordStart = wStart;
>   maxLength = wLength;
> }
> }
> strncpy( longestString , file + longestWordStart , maxLength );
> 
> This version takes 0.005 seconds.

Nice. I especially like the way it'll segfault if there is a blank at 
the end of the file.

Roman



More information about the Haskell-Cafe mailing list