I understand a little bit about IO. I understand that you can use readFile to get the content of a file. For example like this:
main = do
let inputFilePath = "C:\\Haskell\\myawesomeprogram\\files\\a.txt"
content <- readFile inputFilePath
print content
Calling the program:
> runghc myawesomeprogram
"AAA"
Awesome, that works! Now I want to read the content from multiple files. I tried something like this:
files = ["C:\\Haskell\\myawesomeprogram\\files\\a.txt", "C:\\Haskell\\myawesomeprogram\\files\\b.txt","C:\\Haskell\\myawesomeprogram\\files\\c.txt"]
main :: IO ()
main = do
filesContent <- readFiles files
print filesContent
readFiles (x:xs) = do
content <- readFile x
content : readFiles xs
This will give me the following error message:
myawesomeprogram.hs:6:21: error:
* Couldn't match type `[]' with `IO'
Expected type: IO String
Actual type: [String]
* In a stmt of a 'do' block: filesContent <- readFiles files
In the expression:
do filesContent <- readFiles files
print filesContent
In an equation for `main':
main
= do filesContent <- readFiles files
print filesContent
|
6 | filesContent <- readFiles files
| ^^^^^^^^^^^^^^^
myawesomeprogram.hs:9:1: error:
Couldn't match type `IO' with `[]'
Expected type: [FilePath] -> [String]
Actual type: [FilePath] -> IO String
|
9 | readFiles (x:xs) = do
| ^^^^^^^^^^^^^^^^^^^^^^^...
myawesomeprogram.hs:11:5: error:
* Couldn't match type `[]' with `IO'
Expected type: IO String
Actual type: [String]
* In a stmt of a 'do' block: content : readFiles xs
In the expression:
do content <- readFile x
content : readFiles xs
In an equation for `readFiles':
readFiles (x : xs)
= do content <- readFile x
content : readFiles xs
|
11 | content : readFiles xs
| ^^^^^^^^^^^^^^^^^^^^^^
I'm doing something wrong, however, I can't see a way to do it right. Can you do it the right way?