我有兴趣尽可能接近以下语法。我很好。
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 ()
答案 0 :(得分: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"