Expressing do block using only monadic bind syntax

后端 未结 2 2034
难免孤独
难免孤独 2020-12-02 02:18

As far as I know, do blocks in Haskell are just some kind of syntactic sugar for monadic bind operators. For example, one could convert



        
2条回答
  •  半阙折子戏
    2020-12-02 02:26

    Yes, all of them can be converted to bind syntax; in fact, they are converted internally by the compiler.

    I hope this translation of your example gives you the hint:

    main = readFile "foo.txt" >>= \f ->
           (print $ "prefix " ++ f) >>
           (print $ f ++ " postfix")
    

提交回复
热议问题