Working through tutorials etc. in ghci - so far so good. I'm so completely missing something though : my function builds an IO [FilePath]
"thing". In ghci it comes out like this:
["xml","velocity.log.1","velocity.log"]
(list truncated for brevity)
I see that the function is doing what I want. Next step is I want to "print" that out myself.
Nothing I do lets me print the result. I don't want to perpetuate my Java/C#/Python habits in Haskell - no point in that. I believe there's a good reason for Haskell doing things differently, but I can't see how to get the (limited) value out of this function.
module Main (
main
) where
import RecursiveContents
main = do putStrLn "this"
getRecursiveContents "/home/xyz/myDir"
This works. But what if I want main to print the result of getRecursiveContents "/home/xyz/myDir"
?
In ghci I can just type/paste getRecursiveContents "/home/xyz/myDir"
and the stuff spews out - what do I have to do to print it myself?
If I do :
let xyz = getRecursiveContents "/home/xyz/myDir"
in ghci, the only thing I can do with xyz
is type:
xyz <enter>
and see the result.
I cannot do head, tail, etc. etc.. I know that IO [FilePath]
is something special and not a the same as array or list [a]
- but nothing I do is helping me to understand getting past this.
I must be missing something - something I can't find in Learn You a Haskell, or Real World Haskell. Am I not rtfm-ing in the right place?
Any feedback or dope-slaps appreciated.
To get the results of an IO action (i.e. to run the action) you bind the results of the IO
computation to a variable:
Assuming:
getRecursiveContents :: FilePath -> IO String
Then you can just print the result:
main = do str <- getRecursiveContents "/home/xyz/myDir"
print str
Obviously this is just an example, but when the function really is just two lines people don't usually use do
notation and avoid explicitly naming the intermediate variable of str
:
main = getRecursiveContents "/home/xyz/myDir" >>= print
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With