Haskell 不应跳过保护时,会跳过保护
我有以下代码片段Haskell 不应跳过保护时,会跳过保护,haskell,semantics,Haskell,Semantics,我有以下代码片段 module Main where main :: IO() main = do ne <- getLine c <- getLine putStrLn $ show $ foo c (words ne) foo :: String -> [String] -> Integer foo c (n:e:_) = foo' (read c::Integer) (read e::Integer) (read n::Integer)
module Main where
main :: IO()
main = do
ne <- getLine
c <- getLine
putStrLn $ show $ foo c (words ne)
foo :: String -> [String] -> Integer
foo c (n:e:_) =
foo' (read c::Integer) (read e::Integer) (read n::Integer) [2..]
where foo' c e n (x:xs)
| mod (x^e) n == c = mod x n
| otherwise = foo' c e n xs
跳过第一个保护并进入无限循环。
我的看法是
foo'
首先应使用293
调用,这将导致mod(2^9)3==2
这是真的,并应导致值mod29
,但情况并非如此
我确信我在这里遗漏了一些琐碎的东西,但我就是看不到…在foo的定义中,e和n的位置是错误的(n在foo中位于e之前,但在foo中则相反)。所以你不是通过293,你是通过239。
foo“2”[“9”,“3”]
将导致foo'239[2..]
,而不是foo'293[2..]
。你的论点弄混了吗?总是有些琐碎的事情。。。谢谢
9 3
2