2

xmonad我在包中看到以下代码:

-- | Ignore SIGPIPE to avoid termination when a pipe is full, and SIGCHLD to
-- avoid zombie processes, and clean up any extant zombie processes.
installSignalHandlers :: MonadIO m => m ()
installSignalHandlers = io $ do
    installHandler openEndedPipe Ignore Nothing
    installHandler sigCHLD Ignore Nothing
    (try :: IO a -> IO (Either SomeException a))
      $ fix $ \more -> do
        x <- getAnyProcessStatus False False
        when (isJust x) more
    return ()

看来这个fix函数来自Data.Function

但我不明白它是如何在这里使用的,什么时候有人会使用这个修复功能?

4

1 回答 1

5

fix是用于实现递归的基本工具。它总是可以被递归替换let——反之亦然,递归let可以变成对fix. 在这个例子中,

fix $ \more -> do
    x <- getAnyProcessStatus False False
    when (isJust x) more

相当于

let more = do
        x <- getAnyProcessStatus False False
        when (isJust x) more
in more

并且本质上是实现一个循环调用getAnyProcessStatus False False,直到它返回Nothing

于 2018-09-18T16:49:32.060 回答