[Haskell-beginners] Int and Integer

Peter Normington peter at ireby.org.uk
Sun Dec 17 13:48:44 UTC 2017


Why do I get an error in the following example:

memoized_fib :: Integer -> Integer
memoized_fib = (map fib [0 ..] !!)
  where fib 1 = 1
        fib 2 = 1
        fib n = memoized_fib (n-2) + memoized_fib (n-1)

but not in:

memoized_fib :: Int -> Integer
memoized_fib = (map fib [0 ..] !!)
  where fib 1 = 1
        fib 2 = 1
        fib n = memoized_fib (n-2) + memoized_fib (n-1)
?

The error is reported by GHCi as:

error:
    • Couldn't match type ‘Integer’ with ‘Int’
      Expected type: Integer -> Integer
        Actual type: Int -> Integer
    • In the expression: (map fib [0 .. ] !!)
      In an equation for ‘memoized_fib’:
          memoized_fib
            = (map fib [0 .. ] !!)
            where
                fib 1 = 1
                fib 2 = 1
                fib n = memoized_fib (n - 2) + memoized_fib (n - 1)
  |
2 | memoized_fib = (map fib [0 ..] !!)
  |                 ^^^^^^^^^^^^^^^^^
Failed, 0 modules loaded.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171217/fd046800/attachment.html>


More information about the Beginners mailing list