So I work with files, and I need to know the largest line in file X. Using Unix awk results in a Int that I’m looking for. But in Haskell how can I return that value and save it to a variable?
I tried define something with IO [Int] -> [Int]
maxline = do{system "awk ' { if ( length > x ) { x = length } }END{ print x }' filename";}
doesn’t work cause:
Couldn't match expected type 'Int',against inferred type 'IO GHC.IO.Exception.ExitCode'
This is because the
systemaction returns the exit status of the command you run which cannot be converted toInt. You should use thereadProcessto get the commands output.Note that
readProcessdoes not pass the command to the system shell: it runs it directly. The second parameter is where the command’s arguments should go. So your example should be