Haskell - help with using contents read from a file

Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 29
Reputation: artemis_f is an unknown quantity at this point 
Solved Threads: 0
artemis_f artemis_f is offline Offline
Light Poster

Haskell - help with using contents read from a file

 
0
  #1
Mar 28th, 2009
I normally program in Java and Haskell to me seems completely alien and weird.

All I want to do is read the contents of a file into a big string then be able to do stuff with that string like split it on newline and store it in a list then split each line in that list with a " " and store them somewhere but I have read and read online without any sort of enlightenment. Apparently I cannot convert an IO String to a String? But then how am I ever going to be able to use the functions I have written that need an input String?

What I have so far...
read :: String -> IO ()
read input = do
	inh <- openFile input ReadMode
	inputString <- hGetContents inh
	putStr (storeReadFile inputString)
	hClose inh 

storeReadFile :: String -> String
storeReadFile input = input

split :: String -> Char -> [String]
split [] delim = [""]
split (c:cs) delim
   | c == delim = "" : rest
   | otherwise = (c : head rest) : tail rest
   where
       rest = split cs delim

I guess I eventually want to be able to say in some main method that you call the read function use the value it gets (ie the inputString into which the contents of the file were read) then split it on the newline and store that in a list ...then do something with that list etc.

How can I do this?


Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

Re: Haskell - help with using contents read from a file

 
0
  #2
Mar 28th, 2009
Originally Posted by artemis_f View Post
I normally program in Java and Haskell to me seems completely alien and weird.
That's because it is. I normally program in Haskell and Java seems deficient and limiting. I don't think you really get a benefit out of learning Haskell until you learn more about type classes. But the benefit is huge.

All I want to do is read the contents of a file into a big string
Note that there is a function, readFile :: FilePath -> IO String , that does the opening and closing for you.

then be able to do stuff with that string ... Apparently I cannot convert an IO String to a String? But then how am I ever going to be able to use the functions I have written that need an input String?
He he he he he he he.

If I want to read a file and pass it to a function that needs a String, it's as simple as this:

countSpaces :: String -> Int
countSpaces s = length (filter (== ' ') s)

countSpacesInFile :: String -> IO Int
countSpacesInFile fileName = do
  text <- readFile fileName
  return (countSpaces text)

-- our program counts the number of spaces in input.txt
main :: IO ()
main = do
  n <- countSpacesInFile "input.txt"
  print n

In fact, you were already doing that, no? You were reading the contents of the file and passing it to the function storeReadFile, which happened to be doing nothing to it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 29
Reputation: artemis_f is an unknown quantity at this point 
Solved Threads: 0
artemis_f artemis_f is offline Offline
Light Poster

Re: Haskell - help with using contents read from a file

 
0
  #3
Mar 29th, 2009
!!! Thanks so much for getting me started on this.

I guess Haskell is just a different way of thinking - one I am just not used to. I have been writing some more things and just the amount of little things I have to define - like how to count some instead of just using .size like I do in Java etc makes me annoyed. But I am sure there are several benefits to functional programming that I just haven't discovered.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Legacy and Other Languages Forum


Views: 1309 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Legacy and Other Languages
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC