4 ms·
Haskell: import System.Environment import Data.List main = do infile:outfile:_ <- getArgs input <- readFile infile writeFile outfile . unlin
by crntaylor 13y ago
Haskell:
import System.Environment
import Data.List
main = do
infile:outfile:_ <- getArgs
input <- readFile infile
writeFile outfile . unlines . sort . lines $ input
- dmytrish 13y agoIt's a bit odd way of matching items of a list, I'd prefer to specify it without any possible trailing items: [infile, outfile] <- getArgs Also, the last two lines may be squeezed into one (though less readable): writeFile outfile =<< (unlines . sort . lines) `fmap` readFile infile
- crntaylor 13y agoOn your first point; I was matching the behaviour of the C++ program in the blog post, which allows any number of command line arguments but only uses the first two. On your second point, I actually thought about writing the whole thing as getArgs >>= \a -> readFile (a!!0) >>= writeFile (a!!1) . unlines . sort . lines but decided that that's exactly the kind of thing that gets Haskell programmers a bad reputation.
- nightski 13y agoAgreed, it does and you made the right choice. But in reality once you know Haskell - bind is a very common operation and reading this version is very natural.