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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:49:18+00:00 2026-05-14T00:49:18+00:00

Suppose I’m writing a function that takes a list of integers and returns only

  • 0

Suppose I’m writing a function that takes a list of integers and returns only those integers in the list that are less than 5.2. I might do something like this:

belowThreshold = filter (< 5.2)

Easy enough, right? But now I want to constrain this function to only work with input lists of type [Int] for design reasons of my own. This seems like a reasonable request. Alas, no. A declaration that constraints the types as so:

belowThreshold :: [Integer] -> [Integer]
belowThreshold = filter (< 5.2)

Causes a type error. So what’s the story here? Why does doing filter (< 5.2) seem to convert my input list into Doubles? How can I make a version of this function that only accepts integer lists and only returns integer lists? Why does the type system hate me?

  • 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-05-14T00:49:19+00:00Added an answer on May 14, 2026 at 12:49 am

    Check the inferred type of belowThreshold in ghci before adding your annoatation:

    > :t belowThreshold
    belowThreshold :: [Double] -> [Double]
    

    It sounds like you expected Num a => [a] -> [a] when you said “constrain this function”. You are actually changing the type of the function when you add the [Integer] -> [Integer] annotation.

    To make this work, use an explicit conversion:

    belowThreshold = filter ((< 5.2) . fromIntegral)
    

    Now belowThreshold :: [Integer] -> [Integer] like you wanted. But the integers are converted to doubles before comparison to 5.2.

    So why do you need the conversion? The type error probably misled you: the list of Integers wasn’t being converted to Doubles by comparison to 5.2, the real problem is that only Doubles can be compared to Doubles, so you must pass a list of Doubles to belowThreshold. Haskell has no implicit conversions, not even between numbers. If you want conversions, you have to write them yourself.

    I want to constrain this function to only work with input lists of type [Int] for design reasons of my own. This seems like a reasonable request.

    Well, from the perspective of the type system, no. Is this reasonable code?

    'c' < "foo"
    

    What about this?

    12 < "bar"
    

    All of these values are instances of Ord, but you can’t use them together with (<). Haskell has no implicit conversions. So even if two values are both instances of Num as well as Ord, you won’t be able to compare them with (<) if they are of different types.

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

Sidebar

Related Questions

Suppose I have a FruitDetector class which takes in a string and returns the
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose you're developing a software product that has periodic releases. What are the best
Suppose I need to create my own small DSL that would use Python to
Suppose that I have the following code: private void UpdateDB(QuoteDataSet dataSet, Strint tableName) {
Suppose you have the following... An ASP.NET web application that calls a stored procedure
Suppose that I want to customize the indentation rules of the foton document editor,
Suppose that I have an n -sided loaded die, where each side k has
Suppose i have an XML file, that i use as local database, like this):

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.