Haskell - pattern matching and exitSuccess

I have a function that looks like this:

outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] -> Int -> IO ()
outputDelayCo Nothing  = outputDelay Nothing 
outputDelayCo (Just 1) = do exitSuccess
outputDelayCo (Just n) = outputDelay (Just (n-1)) 

I get this error:

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 }

I can fix this by doing this, but it is much uglier:

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

I understand why the error occurs: exitSuccess will have an IO I / O type, so the types do not match this pattern. But what is the right / elegant way to do this?

+4
source share
2 answers

Use lambda for the line exitSuccess:

outputDelayCo (Just 1) = \_ _ _ -> exitSuccess

Please note that is doalso redundant in this case, since it is do fooequivalent foo. A designation dois only useful when you have a sequence of actions.

+6
source

, const:

outputDelayCo (Just 1) = const . const . const $ exitSuccess

, - , , , Ganesh, , .

+2

All Articles