tags:

views:

74

answers:

2

I have the following snippet of code

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

Which works as expected except when given the following input:

9 3 
2

The first guard is skipped and an infinite loop is entered. The way I see this is that foo' should first be called with 2 9 3 which would result in mod (2^9) 3 == 2 which is true and should result in the value mod 2 9 but this is not the case.

I am sure I am missing something trivial here, but I just cant see it...

+1  A: 

You have e and n the wrong way round in the definition of foo' (n comes before e in foo, but it's the other way around in foo'). So you are not passing 2 9 3, you're passing 2 3 9.

Dan Dyer
Always something trivial... Thanks!
ezpz
+1  A: 

foo "2" ["9","3"] will result in foo' 2 3 9 [2..], not foo' 2 9 3 [2..]. Got your arguments mixed up?

spookylukey