不应该跳过警卫

时间:2009-10-07 11:25:12

标签: 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) [2..]
   where foo' c e n (x:xs)
      | mod (x^e) n == c = mod x n
      | otherwise = foo' c e n xs

除非给出以下输入,否则其工作正常:

9 3 
2

跳过第一个警卫并输入无限循环。 我看到的方式就是这样 首先应使用foo'调用2 9 3,这将导致mod (2^9) 3 == 2为真,并且应该生成值mod 2 9,但事实并非如此。

我确信我在这里遗漏了一些微不足道的东西,但我无法看到它......

2 个答案:

答案 0 :(得分:1)

e n foo' n foo 中的 e ,但在 foo'中却是另一种方式。所以你没有通过2 9 3,你通过2 3 9。

答案 1 :(得分:1)

foo "2" ["9","3"]会产生foo' 2 3 9 [2..],而非foo' 2 9 3 [2..]。你的论点搞砸了吗?

相关问题