1

我有兴趣尽可能接近以下语法。TH对我来说很好。

bootapplication :: IO ()
bootapplication = do
  clientA <- newChan :: IO (Chan AMsg)
  clientB <- newChan :: IO (Chan BMsg)
  ...
  magicHappens


doSomething :: SomeSortaMonadTOnIO ()
doSomething = do
  writeChan clientA aMsg
  writeChan clientB bMsg

我想以下是可以接受的

main = do
    clientA <- newChan :: IO (Chan [Char])
    clientB <- newChan :: IO (Chan Whatever)
    let a = putStrLn . (++) "a"
    let moof = do
--      b "hello"
        a "hi"
--      d "meh"
        readChan clientA
        return ()
    let b = putStrLn . (++) "b"
    let d = putStrLn . (++) "c" 
    return ()

但是注释行是不可接受的,因为 let 绑定只能继续进行。我如何锤击和胶带 ghc 让我这样做?

编辑:

这是我确定的语法

main = do
    clientA <- newChan :: IO (Chan [Char])
    clientB <- newChan :: IO (Chan Whatever)
    let {

a :: [Char] -> IO ()
;a = putStrLn . (++) "a"

;moof :: IO ()
;moof = do 
    a "a"
    b "b"

;b :: [Char] -> IO ()
;b = putStrLn . (++) "b"

;d :: [Char] -> IO ()
;d = putStrLn . (++) "c"


}
    moof
    return ()
4

1 回答 1

2

回答您的确切问题,只需将这些行合并到一个let块中:

main = do
  clientA <- newChan :: IO (Chan [Char])
  clientB <- newChan :: IO (Chan Whatever)
  let 
    a = putStrLn . (++) "a"
    moof = do
      b "hello"
      a "hi"
      d "meh"
      readChan clientA
      return ()
    b = putStrLn . (++) "b"
    d = putStrLn . (++) "c" 
  return ()

或者,您可以使用where更传统的方法来存储函数定义,就像您的情况一样:

main = do
  clientA <- newChan :: IO (Chan [Char])
  clientB <- newChan :: IO (Chan Whatever)
  let 
    moof = do
      b "hello"
      a "hi"
      d "meh"
      readChan clientA
      return ()
  return ()
  where
    a = putStrLn . (++) "a"
    b = putStrLn . (++) "b"
    d = putStrLn . (++) "c"

从上面的代码很容易追踪到moof实际上永远不会被执行,因为块let a = b内部do只是a <- return b. 所以我猜你的意图实际上是:

main = do
  clientA <- newChan :: IO (Chan [Char])
  clientB <- newChan :: IO (Chan Whatever)
  moof <- do
    b "hello"
    a "hi"
    d "meh"
    readChan clientA
    return ()
  return ()
  where
    a = putStrLn . (++) "a"
    b = putStrLn . (++) "b"
    d = putStrLn . (++) "c"
于 2013-05-23T08:13:21.013 回答