I'm pretty new to Haskell, and want to execute recursion in do block.
countLines :: String -> IO Int
countLines filePath = do
isFile <- doesFileExist filePath
if isFile
then do contents <- readFile filePath
print contents
pure 0
else do files <- getDirectoryContents filePath
[countLines(file) | file <- files] -- recursion here!
pure 0
until i add this list comprehension, everything works fine but once i add this, i get the following error:
Main.hs:59:17: error:
• Couldn't match type ‘[]’ with ‘IO’
Expected type: IO (IO Int)
Actual type: [IO Int]
• In a stmt of a 'do' block: [countLines (file) | file <- files]
In the expression:
do files <- getDirectoryContents filePath
[countLines (file) | file <- files]
pure 0
In a stmt of a 'do' block:
if isFile then
do contents <- readFile filePath
print contents
pure 0
else
do files <- getDirectoryContents filePath
[countLines (file) | file <- files]
pure 0
|
59 | [countLines(file) | file <- files]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bash-3.2$
does any one know how to fix this?
[ countFile(file) | file <- files ]) into an action that executes them all using thesequencefunction