
Hi, Trying to get aprogram to count all the lines or words from a file. Not sure about the syntax. Probably missing some. Here's what I have so far any help in the right direction would be great. import System.IO main :: IO () main = do incom <- openFile "file.txt" ReadMode otcom <- openFile "prob.txt" WriteMode fCount incom otcom hClose incom hClose otcom fCount incomh otcomh = do eof <- hIsEof incomh if eof then return() else do c <- hGetCount incom it tells me parse error on = John

On Sun, Oct 18, 2009 at 7:52 AM, John Moore
Hi, Trying to get aprogram to count all the lines or words from a file. Not sure about the syntax. Probably missing some. Here's what I have so far any help in the right direction would be great.
import System.IO main :: IO () main = do incom <- openFile "file.txt" ReadMode otcom <- openFile "prob.txt" WriteMode fCount incom otcom hClose incom hClose otcom fCount incomh otcomh = do eof <- hIsEof incomh if eof then return() else do c <- hGetCount incom
it tells me parse error on =
John
Add a "where" before fCount - this lets you introduce auxiliary functions. Alternatively, you could define fCount at the top level. The way I would count all of the lines in a file is (untested) fCountLines :: String -> IO Int fCountLines = length . lines . readFile Hope that helps. Alex

On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap < alexander.dunlap@gmail.com> wrote:
The way I would count all of the lines in a file is (untested)
fCountLines :: String -> IO Int fCountLines = length . lines . readFile
I'm not sure if I did something wrong, but I could not get this to work because 'readFile' returns an IO String. I tried the following and it worked. fileLines str = readFile str >>= return . lines >>= return . length Is this a reasonable way to count lines?

Am Sonntag 18 Oktober 2009 18:40:48 schrieb Robert Ziemba:
On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap <
alexander.dunlap@gmail.com> wrote:
The way I would count all of the lines in a file is (untested)
fCountLines :: String -> IO Int fCountLines = length . lines . readFile
I'm not sure if I did something wrong, but I could not get this to work because 'readFile' returns an IO String. I tried the following and it worked.
fileLines str = readFile str >>= return . lines >>= return . length
Is this a reasonable way to count lines?
Almost. It should be fileLines str = readFile str >>= return . length . lines (or return . length . lines =<< readFile str or fmap (length . lines) (readFile str) or...)

On Sun, Oct 18, 2009 at 06:51:41PM +0200, Daniel Fischer wrote:
fileLines str = readFile str >>= return . length . lines
This kind of construction requires a Monad, which is a lot of power.
fmap (length . lines) (readFile str)
On the other hand, this only requires a Functor. Yes, in this particular case the type is going to be IO anyway, but 'fmap' should be used instead of '>>= return .'. -- Felipe.
participants (5)
-
Alexander Dunlap
-
Daniel Fischer
-
Felipe Lessa
-
John Moore
-
Robert Ziemba