Binding Monads in Composites (Haskell)

Good afternoon. I am looking for some clarification on monads, please and using bind (at the entrance to composite .), please.

So for this example:

--Monadic parts:
readFile :: String -> IO File
putStr :: String -> IO()
-- Non monadic parts
toMatrix :: String -> CustomMatrix
toString :: CustomMatrix -> String

Essentially, I lazily read the file ( readFile) and then create a custom matrix, converting the matrix to line output. Then come back.

fileReading :: String -> IO
fileReading file = putStr(toString . toMatrix . readFile file)

This is when I start creating a mess using bind >>=to jump from readFile file. Is there a way I can continue to use composites .and bind and compose without making an illegible mess (not my goal).

As always, any help is greatly appreciated. Thanks guys.

+5
source share
1 answer

, (, >>=). -, -.

, :

fileReading file = readFile file >>= putStr . toString . toMatrix

, . - -. -: IO String String -> IO () ( putStr . toString . toMatrix ). , >>=, .

, , , :

fileReading file = putStr . toString . toMatrix =<< readFile file

Some people find this version more readable because all the code "flows" in one direction.

+7
source

All Articles