我有一個類似如下的功能:哈斯克爾 - 模式匹配和exitSuccess
outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] -> Int -> IO()
outputDelayCo Nothing = outputDelay Nothing
outputDelayCo (Just 1) = do exitSuccess
outputDelayCo (Just n) = outputDelay (Just (n-1))
我得到這個錯誤:
Couldn't match expected type ‘(Event -> ByteString)
-> [Event] -> Int -> IO()’
with actual type ‘IO a0’
In a stmt of a 'do' block: exitSuccess
In the expression: do { exitSuccess }
我可以這樣做修復它,但它更醜陋:
outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] -> Int -> IO()
outputDelayCo Nothing a b c = outputDelay Nothing a b c
outputDelayCo (Just 1) _ _ _ = do exitSuccess
outputDelayCo (Just n) a b c = outputDelay (Just (n-1)) a b c
我明白爲什麼有一個錯誤:做exitSuccess是要去有一個IO返回類型,所以類型不上拍匹配燕鷗。但是,什麼是正確的/優雅的方式來做到這一點?