为了练习Haskell,我实现了Fermat的因式分解方法(参见https://en.wikipedia.org/wiki/Fermat%27s_factorization_method)。然而,当我运行我的程序,哈斯克尔不断告诉我:haskell:调试<<loop>>异常
$ ./fermat 7
fermat: <<loop>>
如此看来,有一个在我的代码无限循环(CMP http://www.haskell.org/pipermail/haskell-cafe/2013-June/108826.html)。任何人都可以给我一个提示,我做错了什么?
另外,我想扩展问题How to debug Haskell code?以获得有关如何调试此特定异常的提示。
import Data.List
import System.Environment
import Debug.Trace
isQuad :: Integer -> Bool
isQuad x = a == b
where
a = ceiling $ s
b = floor $ s
s = sqrt (fromIntegral x :: Double)
test :: Integer -> Integer -> Integer -> Bool
test nr n s = trace(show nr ++ " " ++ show n ++ " " ++ show s)
isQuad(
(+)
((\j -> j * j) s + nr)
(-n)
)
fermat :: Integer -> (Integer, Integer)
fermat n = (s + x, s - x)
where
s = ceiling $ sqrt (fromIntegral x :: Double)
r = trace
(show s ++ " " ++ show n)
(\(Just i) -> i) $
find
(\nr -> test nr n s)
[0..n]
x = floor $ sqrt (fromIntegral r :: Double)
fact :: Integer -> (Integer, Integer)
fact x
| x == 1 = (1, 1)
| even x = (2, x `div` 2)
| otherwise = fermat x
f :: String -> String
f x = x ++ " = " ++ show a ++ " x " ++ show b
where
(a, b) = fact $ (read x :: Integer)
main = do
args <- getArgs
putStrLn $ unlines $ map f args