[Haskell-beginners] Int and Integer

Theodore Lief Gannon tanuki at gmail.com
Sun Dec 17 15:40:54 UTC 2017


Because:
(!!) :: [a] -> Int -> a

On Dec 17, 2017 5:50 AM, "Peter Normington" <peter at ireby.org.uk> wrote:

> 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.
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171217/5c82bb66/attachment.html>


More information about the Beginners mailing list