Haskell: Constrain function on type Double to only work with Integers

Posted by thurn on Stack Overflow See other posts from Stack Overflow or by thurn
Published on 2010-03-14T16:33:22Z Indexed on 2010/03/14 16:35 UTC
Read the original article Hit count: 173

Filed under:

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?

© Stack Overflow or respective owner

Related posts about haskell