什么阻止了我的代码中的IO刷新?

时间:2017-03-24 06:15:03

标签: haskell

我尝试解决this,以下是试用版。

当我使用hSetBuffering stdout NoBufferingsolveAct 115 10在ghci中对此进行测试时,ghci显示几行结果并阻止了很多时间,并立即显示其余结果。

如何实时查看中间结果?

import Control.Monad
import Data.List
import Data.Maybe
import System.IO

readInts = fmap read . words <$> getLine :: IO [Int]

main = do
  t <- readLn :: IO Int
  hSetBuffering stdout NoBuffering
  sequence_ $ solveAct <$> [1..t]

showTable x = intercalate "\n" $ intercalate " " . fmap show <$> x

solveAct i = do
  [j, n] <- readInts
  putStrLn $ "Case #" ++ show i ++ ":"
  putStrLn $ showTable (take n $ solve (j-1))

digits n = [[x ^ y | y <- [1..n-1]] | x <- [2..10]]

primes = 2 : [x | x <- [3,5..], all (\y -> x `rem` y /= 0) $ takeWhile (<= intSqrt x) primes]

intSqrt = floor . sqrt . fromIntegral
getNDivisor n = listToMaybe [x | x <- takeWhile (<= intSqrt n) primes, n `rem` x == 0]

casesOfMat = subsequences . transpose . digits
casesOfJam n = fmap ([1 + x^n | x <- [2..10]]:) $ casesOfMat n
eachBaseReps n = fmap sum . transpose <$> casesOfJam n

solve :: Int -> [[Int]]
solve n = do
  decimals <- eachBaseReps n
  let divs = getNDivisor <$> decimals
  guard $ all isJust divs
  return $ last decimals : catMaybes divs

1 个答案:

答案 0 :(得分:1)

实时查看结果。只是all isJust . map getNDivisor的计算需要花费很长时间才能获得eachBaseReps 14的第三个元素。