[Haskell-cafe] One question...

Donald Bruce Stewart dons at cse.unsw.edu.au
Sun Dec 10 04:55:50 EST 2006


wb:
> Thank you all of you,
> 
> your remarks and commants are very useful for me. 
> I'm starting to studying Haskell!
> 
> My first comparison to (my preferred) Perl is to show file on 
> the screen:
> 
> The Code in Haskell:
> 
> =====================
> import System
> main=do
> 	[ f ] <- getArgs
> 	s <- readFile f
> 	putStr s
> =====================
> 
> is more readable but a longer than that in Perl:
> 
> =====================
> open f, "<$ARGV[0]";
> while ( <f> ) { print }
> =====================
> 
> Is it possible to make the Haskel code shorter?

    import System
    main = getArgs >>= readFile . head >>= putStr

If you prefer to read from standard input:

    main = interact id

is enough. A variety of other unix tools are here:

    http://haskell.org/haskellwiki/Simple_unix_tools

> How to join the three line in one? Is it possible?
> What about the check if there exists referred file?
> In Perl it could look like:
> =====================
> if ( open f, "<$ARGV[0]" ) {
> 	while ( <f> ) { print }
> }
> =====================
> and is still shorter than that in Haskell. 
> 
> I have to admit that I think that as shorter source code as better.
> Therefore the question to those who know Perl: is the source code of a 
> client-server application shorter in Haskel to compare to Perl one? 
> How much shorter (more or less)? 
> Is that source code really easier to debug (in OO Perl it is not really 
> easy :-( 

Don't worry, as your programs get larger, the code will tend to stay
shorter than in other languages. Pattern matching, better abstractions,
simpler data types all add up to less code.

-- Don


More information about the Haskell-Cafe mailing list