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

  • Home
  • SEARCH
  • 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 8059227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:36:15+00:00 2026-06-05T09:36:15+00:00

I am new to Haskell from a C++ and Java background. Occassionally, I have

  • 0

I am new to Haskell from a C++ and Java background. Occassionally, I have trouble with Haskell’s type system. My current error is with this piece of code:

countIf :: (Integral b) => [a] -> (a -> Bool) -> b
countIf [] p = 0
countIf (x:xs) p
  | p x = 1 + countIf xs p
  | otherwise = countIf xs p

isRelativelyPrime :: (Integral a) => a -> a -> Bool
isRelativelyPrime m n = gcd m n == 1

phi :: (Integral a, Integral b) => a -> b
phi n = countIf [1..(n - 1)] (isRelativelyPrime n)

main = print [(n, phi n, ratio) | n <- [1..10], let ratio = (fromIntegral (phi n)) / n]

The error message is

prog.hs:13:60:
    Ambiguous type variable `b' in the constraints:
      `Fractional b' arising from a use of `/' at prog.hs:13:60-85
      `Integral b' arising from a use of `phi' at prog.hs:13:75-79
    Probable fix: add a type signature that fixes these type variable(s)

13:60 is just before the usage of fromIntegral in the let binding in my list comprehension in main. I’m still trying to get used to ghc’s error messages. I am unable to decipher this particular one in order to figure out what I need to change to get my code to compile. Any help will be greatly appreciated. Thanks.

  • 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-05T09:36:17+00:00Added an answer on June 5, 2026 at 9:36 am

    You need to call fromIntegral on n as well since Haskell doesn’t automatically convert from integral types, which you already seem to know since you called fromIntegral (phi n). I make this mistake all the time, not a big deal!

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

Sidebar

Related Questions

I am from OO background and very new to Haskell. In order to print
Im new to Haskell!! I wrote this code: import Data.List inputIndex :: [String] ->
I am relatively new to Haskell, coming from F# (a Microsoft language). I have
I'm relatively new to Haskell with main programming background coming from OO languages. I
I am pretty new to Haskell but I feel like I have a decent
I am new to Haskell and this mixture of Infix and Prefix notation is
I'm new to Haskell so I apologize if this is an easy question, but
Hello I am new at Haskell and i'm having problems trying to get this
recently I'm writing some functions that I take from Haskell and translate into Java.
This is my first posting on SO, and I'm relatively new to Haskell, so

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.