Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:45:08+00:00 2026-06-11T04:45:08+00:00

I have the following Haskell code: — Problem 69 import ProjectEuler phi :: Integer

  • 0

I have the following Haskell code:

-- Problem 69

import ProjectEuler

phi :: Integer -> Integer
phi n = n * product [p - 1 | p <- primeDivisors n] `div` product [p | p <- primeDivisors n]
-- primeDivisors n is a list of the prime divisors of n

maxRatio :: (Int, Int, Double) -> (Int, Int, Double) -> (Int, Int, Double)
maxRatio t1@(_, _, x) t2@(_, _, y)
  | x > y = t1
  | otherwise = t2

main = print (foldl
                maxRatio
                (0, 0, 0.0)
                [(n, phi n, ratio) | n <- [2..max], let ratio = fromIntegral n / (fromIntegral (phi n))]
              )
    where max = 1000

which gives the following error:

Couldn't match expected type `Int' with actual type `Integer'
In the expression: n
In the expression: (n, phi n, ratio)
In the third argument of `foldl', namely
  `[(n, phi n, ratio) |
      n <- [2 .. max],
      let ratio = fromIntegral n / (fromIntegral (phi n))]'

I suspect that in the triple (0, 0, 0.0) the 0’s are type Int. Is 0 always type Int or is ghci deducing the type as Int in this case? If the later, how do I force it to be type Integer instead? Or is there something else that causes this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:45:09+00:00Added an answer on June 11, 2026 at 4:45 am

    Haskell can generally infer the type of numeric literals such as 0 as whatever appropriate type you need them to be. This is because it knows what functions you pass them to; if I have a function phi :: Integer -> Integer, and I call phi 0, Haskell knows that that particular 0 must have been an Integer. It’s also fine if I call a function pho :: Int -> Int with pho 0; that particular 0 is inferred to be an Int.

    However Int and Integer are different types, and there’s no way one particular 0 can be passed to both phi and pho.

    Your issue is simply that the tuples that maxRatio deals with are typed (by you) (Int, Int, Double), but that one such tuple is constructed as (n, phi n, ratio). Since phi takes and returns Integer, the n in that expression has to be an Integer. But then that doesn’t work for maxRatio, so you get the error.

    Depending on which type you actually wanted (Int or Integer), all you need to do is change the type signature of phi or maxRatio so that they’re working with the same kind of number. Haskell will decide that your literally written 0s are whatever numeric type is necessary to make that work, provided there is one that can make it work!

    Note that the error messaged specifically told you that it was n in (n, phi n, ratio) that was expected to be an Int and was actually an Integer. The (0, 0, 0.0) tuple is never mentioned. Often type errors originate somewhere other than where the compiler points you (since all the compiler can do is spot that different chains of inference produce inconsistent requirements on the type of something, with no way to know which part of the whole process is “wrong”), but in this case it did pretty well.

    Haskell gets a (fairly justified) bad rep for inscrutable error messages, but it can help a lot to start from what the compiler is telling you is the problem and try to figure out why the facts it’s complaining about arise from your code. This will be painful at first, but you’ll quickly develop a basic literacy in Haskell’s error messages (at least the more straightforward ones) that will help you spot these kinds of errors really quickly, which makes the compiler a very powerful error-detection system for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following haskell code: fac n = product [1..n] taylor3s w0 f
I have the following Haskell code import Data.Int import System.Environment type Coord = (Int16,
I have a problem with a Haskell code, I have the following: takeMeHere cs
I have the following solution in Haskell to Problem 3 : isPrime :: Integer
I have some troubles with Haskell's type system. Situation: Following program is taking list
I have some Haskell code that does work correctly on an infinite list, but
I have a very strange problem. The following code gives different results when compiled
I have the following Template Haskell code in my module, which is part of
I was trying to translate the following Haskell code to C++: data List t
I have written a script in haskell named testscript with the following code: #!/usr/bin/env

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.