I have a function fib2 which returning two values as Tuple but I want to print the product of tuples returned by function fib2. I am new to Haskell so please explain it to me in a simple way.
This problem is from SPOJ which I have already solved using C and again I am trying it in Haskell:
For each \$n\$, find \$G(n) \mod 1000000007\$, where $$\begin{align*} G(0) &= 0 \\ G(n) &= G(n-1) + f(4n - 1), \qquad n > 0 \\ f(i) &= \mathrm{the\ i^{th}\ Fibobacci\ number} \end{align*}$$
The first line of input specifies the number of test cases.
Now my code is working fine but I'm getting TLE in this problem. Can someone show me how to optimize Fibonacci in Haskell?
import Data.List
import Data.Maybe
import Control.Monad
import qualified Data.ByteString.Char8 as C
fib2 0 = (1, 1)
fib2 1 = (1, 2)
fib2 n
 | even n    = ((a*a + b*b)`mod` 1000000007, (c*c - a*a)`mod` 1000000007)
 | otherwise = ((c*c - a*a)`mod` 1000000007, (b*b + c*c)`mod` 1000000007)
 where (a,b) = fib2 (n `div` 2 - 1)
       c     = a + b
solve n = (a*b)`mod`1000000007
    where (a,b) = fib2((2*n-1)`mod`2000000016)
main = C.getContents >>= putStrLn . unlines . map (show.solve.fromIntegral . fst . fromJust . C.readInteger) . tail . C.words

!c = a + bin thewhereclause offib2(requires{-# LANGUAGE BangPatterns #-}at the top), and a small improvement from usingquotandreminstead ofdivandmod. I'm not sure of the compile options SPOJ passes, so adding{-# OPTIONS_GHC -O2 #-}after the language pragma might be required. \$\endgroup\$-O2as a compilation flag, you absolutely need it. \$\endgroup\$