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
IOcomputation to a variable:Assuming:
Then you can just print the result:
Obviously this is just an example, but when the function really is just two lines people don’t usually use
donotation and avoid explicitly naming the intermediate variable ofstr: