Haskell: not getting this with IO [FilePath]

Work with textbooks, etc. in ghci - so far so good. I don’t lose anything like this: my function creates the IO [FilePath]“thing”. In ghci, this happens as follows:

["xml","velocity.log.1","velocity.log"] (shortened for short)

I see that the function does what I want. The next step I want to “print” is myself.

Nothing I do allows me to print the result. I don't want to perpetuate my Java / C # / Python habits in Haskell - that makes no sense. I believe that a good reason for Haskell to do things differently, but I don’t see how to get the (limited) value from this function.

module Main (
    main
) where

import RecursiveContents

main = do putStrLn "this"
          getRecursiveContents "/home/xyz/myDir"

It works. But what if I want main to print the result getRecursiveContents "/home/xyz/myDir"?

ghci / getRecursiveContents "/home/xyz/myDir", - , ?

:

let xyz = getRecursiveContents "/home/xyz/myDir" ghci , xyz, : xyz <enter> .

, .. .. , IO [FilePath] - , , [a], , , .

- - -, Haskell Real World Haskell. ?

- .

+3
3

- (, ), IO :

, :

getRecursiveContents :: FilePath -> IO String

:

main = do str <- getRecursiveContents "/home/xyz/myDir"
          print str

, , , do str:

main = getRecursiveContents "/home/xyz/myDir" >>= print
+8

<- do-notation, [FilePath]. , - IO.

main = do putStrLn "this"
          contents <- getRecursiveContents "/home/xyz/myDir"
          print contents

>>= IO . , -.

main = do putStrLn "this"
          getRecursiveContents "/home/xyz/myDir" >>= print

. /


, GHCi, , GHCi , . IO a, , . . GHC.

+5

You can bind values ​​in do blocks. This is also a place where you can apply pure functions (e.g. show, tail, etc.):

main = do putStrLn "this"
          x <- getRecursiveContents "/home/xyz/myDir"
          putStrLn (show x)

If foo is of type IO a, then you need something like do and x <- foo to get the value.

Refer to any monad tutorial for more information.

+4
source

All Articles