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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:24:51+00:00 2026-06-16T19:24:51+00:00

I’m writing small hello world type of program, which groups same files by different

  • 0

I’m writing small “hello world” type of program, which groups same files by different “reasons”, e.g. same size, same content, same checksum etc.

So, I’ve got to the point when I want to write a function like this (DuplicateReason is an algebraic type which states the reason why two files are identical):

getDuplicatesByMethods :: (Eq a) => [((FilePath -> a), DuplicateReason)] -> IO [DuplicateGroup]

Where in each tuple, first function would be the one that by file’s path returns you some (Eq a) value, like bytestring (with content), or Word32 with checksum, or Int with size.

Clearly, Haskell doesn’t like that these functions are of different types, so I need to somehow gather them.

The only way I see it to create a type like

data GroupableValue = GroupString String | GroupInt Int | GroupWord32 Word32

And then to make life easier to make typeclass like

class GroupableValueClass a where
  toGroupableValue :: a -> GroupableValue
  fromGroupableValue :: GroupableValue -> a

and implement instance for each value I’m going to get.

Question: am I doing it right and (if no) is there a simpler way to solve this task?

Update:

Here’s full minimal code that should describe what I want (simplified, with no IO etc.):

data DuplicateGroup = DuplicateGroup

-- method for "same size" -- returns size
m1 :: String -> Int
m1 content = 10

-- method for "same content" -- returns content
m2 :: String -> String
m2 content = "sample content"

groupByMethods :: (Eq a) => [(String -> a)] -> [DuplicateGroup]
groupByMethods predicates = undefined

main :: IO ()
main = do
  let groups = (groupByMethods [m1, m2])
  return ()
  • 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-16T19:24:53+00:00Added an answer on June 16, 2026 at 7:24 pm

    Lists are always homogeneous, so you can’t put items with a different a in to the same list (as you noticed). There are several ways to design around this, but I usually prefer using GADTs. For example:

    {-# LANGUAGE GADTs #-}
    
    import Data.ByteString (ByteString)
    import Data.Word
    
    data DuplicateReason = Size | Checksum | Content
    data DuplicateGroup
    
    data DuplicateTest where
        DuplicateTest :: Eq a => (FilePath -> IO a) -> DuplicateReason -> DuplicateTest
    
    getSize :: FilePath -> IO Integer
    getSize = undefined
    
    getChecksum :: FilePath -> IO Word32
    getChecksum = undefined
    
    getContent :: FilePath -> IO ByteString
    getContent = undefined
    
    getDuplicatesByMethods :: [DuplicateTest] -> IO [DuplicateGroup]
    getDuplicatesByMethods = undefined
    

    This solution still needs a new type, but at least you don’t have to specify all cases in advance or create boilerplate type-classes. Now, since the generic type a is essentially “hidden” inside the GADT, you can define a list that contains functions with different return types, wrapped in the DuplicateTest GADT.

    getDuplicatesByMethods
        [ DuplicateTest getSize Size
        , DuplicateTest getChecksum Checksum
        , DuplicateTest getContent Content
        ]
    

    You can also solve this without using any language extensions or introducing new types by simply re-thinking your functions. The main intention is to group files according to some property a, so we could define getDuplicatesByMethods as

    getDuplicatesByMethods :: [([FilePath] -> IO [[FilePath]], DuplicateReason)] -> IO [DuplicateGroup]
    

    I.e. we take in a function that groups files according to some criteria. Then we can define a helper function

    groupWith :: Eq a => (FilePath -> IO a) -> [FilePath] -> IO [[FilePath]]
    

    and call getDuplicatesByMethods like this

    getDuplicatesByMethods
        [ (groupWith getSize, Size)
        , (groupWith getChecksum, Checksum)
        , (groupWith getContent, Content)
        ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have an array which has BIG numbers and small numbers in it. I
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am writing an app for my school newspaper, which is run completely online
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to understand how to use SyndicationItem to display feed which is
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a small JavaScript validation script that validates inputs based on Regex. I
I would like to run a str_replace or preg_replace which looks for certain words

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.