1

I am new to Haskell. I have a code as:

mwave :: Int -> Int
mwave = (map wave [0..] !!)
  where wave 0 = 1
        wave 1 = 1
        wave n = ((3 * n - 3) * mwave (n - 2) + (2 * n + 1) * mwave (n - 1)) `div` (n + 2)

digits :: Int -> Int
digits n = (mwave n) `mod` 10^100::Integer
Output:
wave.exe: divide by zero

I have to output the answer modulo 10^100. How can I do this?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Zubin Kadva
  • 593
  • 1
  • 4
  • 29
  • 1
    Use `Integer` not `Int` – Bergi Sep 21 '16 at 04:38
  • @Bergi When I change the signature of `mwave`, it gives and error: `Couldn't match expected type `Integer' with actual type `Int'` – Zubin Kadva Sep 21 '16 at 15:58
  • 1
    You'd need to change `digits` as well – Bergi Sep 21 '16 at 16:01
  • @Bergi `mwave :: Int -> Integer` `digits :: Integer -> Int` does not work – Zubin Kadva Sep 21 '16 at 16:07
  • 1
    It's `digits :: Int -> Integer` - the return value has the large values, not the parameter – Bergi Sep 21 '16 at 17:10
  • @Bengi. Thanks but it says: `Couldn't match expected type 'Integer' with actual type 'Int'` – Zubin Kadva Sep 21 '16 at 17:43
  • 1
    Where? Please cite the whole error message – Bergi Sep 21 '16 at 18:08
  • Couldn't match expected type 'Integer' with actual type 'Int' In the expression: n 'mod' 10 ^ (100 :: Integer) In an equation for 'digits': digits n = n 'mod' 10 ^ (100 :: Integer) – Zubin Kadva Sep 21 '16 at 18:17
  • 1
    Well that's not the function you posted in your question, no wonder that the type I'm recommending doesn't match. Is `digits` supposed to call `mwave` or not? – Bergi Sep 21 '16 at 18:31
  • Sorry, my bad. I changed the function to test a different case. The actual error is: `Couldn't match expected type 'Integer' with actual type 'Int' * In the expression: (mwave n) 'mod' 10 ^ (100 :: Integer) In an equation for 'digits': digits n = (mwave n) 'mod' 10 ^ (100 :: Integer)` – Zubin Kadva Sep 21 '16 at 18:37
  • 1
    And `mwave` takes an `Int` and returns an `Integer` as well? I had assumed that was already established. – Bergi Sep 21 '16 at 18:42
  • No, `mwave` takes an `Int` and returns an `Int` – Zubin Kadva Sep 21 '16 at 19:22
  • 1
    You had commented you made it `mwave :: Int -> Integer`… It needs to return a large value of course, otherwise it's pointless to try to fit a `10^100` in it. – Bergi Sep 21 '16 at 19:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123887/discussion-between-zubin-kadva-and-bergi). – Zubin Kadva Sep 21 '16 at 19:26

0 Answers0