Do statement under a where clause

前端 未结 4 473
南旧
南旧 2020-12-07 06:35

I\'m trying to convert IO [String] to [String] with <- binding; however, I need to use a do block to

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 07:02

    You cannot convert for IO String to a String.

    What you can do, however, is bind the contents of IO String to a 'variable', but that will still result in the whole computation being embedded inside IO.

    foo = do
       x <- baz -- here baz is the IO String
       let x' = doStuff x
       return x' -- embeds the String inside IO, as otherwise the computation would result in IO ()
    

    To answer your question

    foo x = baz x -- x here is your 'IO String'
      where
        baz x = do
          x' <- x
          return $ doStuff x'
    

提交回复
热议问题