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 8515559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:10:53+00:00 2026-06-11T05:10:53+00:00

I wrote a little haskell program that just counts how many ones there are

  • 0

I wrote a little haskell program that just counts how many ones there are in a number (Int). When I try to execute it haskell complains about ambigous variable constraints.
I know that it comes from the use of floor. I also read some of the answers on stackoverflow. But I didn’t really find a way around that.
Here’s my code:

count_ones = count_ones' 0

count_ones' m 0 = m
count_ones' m n | n-10*n_new == 1 = count_ones' (m+1) n_new
                | otherwise         = count_ones' m n_new
                 where n_new = floor (n/10)

Any advice?

  • 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-11T05:10:55+00:00Added an answer on June 11, 2026 at 5:10 am
    count_ones' m n | n-10*n_new == 0.1 = count_ones' (m+1) n_new
                    | otherwise         = count_ones' m n_new
                     where n_new = floor (n/10)
    

    In the first line, you compare n - 10*n_new to the fractional literal 0.1, so the type of n and n_new must be a member of the Fractional class.

    In the where clause, you bind n_new = floor (n/10), so the type of n_new must be a member of the Integral class.

    Since no standard type is a member of both classes (for good reasons), the compiler can’t resolve the constraint

    (Fractional a, Integral a) => a
    

    when the function is called.

    If you give type signatures to your functions, the compiler can often generate more helpful error messages.

    The simplest fix for your problem is to change the binding of n_new to

    n_new = fromIntegral (floor $ n/10)
    

    Considering that in the comments you said that the 0.1 was a mistake and you should have used 1 instead, you probably want to use Integral types only and the closest transcription of your code would be

    count_ones' :: Integral a => Int -> a -> Int
    count_ones' m 0 = m
    count_ones' m n
        | n - 10*n_new == 1 = count_ones' (m+1) n_new
        | otherwise         = count_ones' m n_new
          where
            n_new = n `div` 10
    

    but it might be clearer to replace the condition n - 10*n_new == 1 with n `mod` 10 == 1.

    However, that would require two divisions per step, which probably is less efficient. Using divMod should give you the quotient and remainder of the division with only one division instruction,

    count_ones' m n = case n `divMod` 10 of
                        (q,1) -> count_ones' (m+1) q
                        (q,_) -> count_ones' m q
    

    and if you can guarantee that you will only call the function with non-negative n, use quot and rem resp. quotRem instead of div and mod resp. divMod. The former functions use the results of the machine division instruction directly, while the latter need some post-processing to ensure that the result of mod is non-negative, so quot and friends are more efficient than div and company.

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

Sidebar

Related Questions

I ported a little Haskell program I wrote from Mac to Windows. It's a
I just wrote a little program which will be executed as a post-build step
I wrote this little program to illustrate my problem : int main(int argc, char*
I wrote a little utility class that saves BitmapSource objects to image files. The
I wrote a little Java servlet that would dynamically generate an image button given
I have just wrote a little example and i can't manage to make it
So I just wrote a little snippet to generate the Mandelbrot fractal and imagine
I'm trying to make a little program in Haskell. What I need to do
I wrote a little program to draw a pattern a cycle through different color
I wrote a little script that check something, and i want a command to

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.