Int and Integer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Int and Integer

Peter Normington
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Int and Integer

Theodore Lief Gannon
Because:
(!!) :: [a] -> Int -> a

On Dec 17, 2017 5:50 AM, "Peter Normington" <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Int and Integer

Peter Normington
In reply to this post by Peter Normington
Thanks, Theodore.  As simple as that!
Best regards,
Peter

On 17 December 2017 at 15:40, Theodore Lief Gannon <[hidden email]> wrote:
Because:
(!!) :: [a] -> Int -> a

On Dec 17, 2017 5:50 AM, "Peter Normington" <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners