| | |
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:
Solved Threads: 0
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...
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
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 delimI 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
•
•
Join Date: Mar 2009
Posts: 139
Reputation:
Solved Threads: 12
•
•
•
•
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
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?
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.
•
•
Join Date: Aug 2008
Posts: 29
Reputation:
Solved Threads: 0
!!! 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.
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.
![]() |
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: How to create a project file in xml?
- Next Thread: Urgent help needed here.
Views: 1309 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Legacy and Other Languages





