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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:09:39+00:00 2026-06-15T20:09:39+00:00

Recently I needed to compare two sets of historical data. Since sometimes a day

  • 0

Recently I needed to compare two sets of historical data. Since sometimes a day or two was missing in one of them and I wanted to be precise, I decided to create a list of all possible dates and two lists of tuples containing dates and corresponding values belonging to both sets. Then I changed the latter lists to Maps in order to improve date lookups.

The idea was to try to find every date from the full dates list in both Mapped list and create a list of “triples” of (date, value1, value2) containing only dates where both data sets had a date and a value. Then I could write them to a file and properly compare them.

DON’T MIND THE CODE, IT’S INCLUDED ONLY FOR GOOD MEASURE

Here is the code (it is not optimal at all, but for that small task it did its job nicely):

import qualified Data.Map as M
import Data.List (transpose)
import Data.Maybe (fromJust)

main = do
    dts     <- readFile "dates.txt"
    cts1    <- readFile "eu.txt"
    cts2    <- readFile "usa.txt"
    let
        dates  = lines dts
        cols1  = transpose $ map words $ lines cts1
        cols2  = transpose $ map words $ lines cts2
        prs1   = zip (head cols1) (last cols1)
        prs2   = zip (head cols2) (last cols2)
        map1   = M.fromList prs1
        map2   = M.fromList prs2
        trips  = map fromJust (filter (/=Nothing) (map (\date -> getTrips date map1 map2) dates))
        cols3  = map (\(a,b,c) -> [a,b,c]) trips
        result = unlines $ map unwords $ cols3
    writeFile "trips.txt" result

getTrips :: String -> M.Map String String -> M.Map String String -> Maybe (String, String, String)
getTrips date map1 map2
    | is1 /= Nothing && is2 /= Nothing    = Just (date, fromJust is1, fromJust is2)
    | otherwise                           = Nothing
    where
        is1 = M.lookup date map1
        is2 = M.lookup date map2

TL;DR: The code worked (though I would gladly hear some opinions/advice), but I have some questions:

  • there were only around 2000 dates, therefore I didn’t care much about performance (you can see that I was using Strings everywhere); was using Data.Map an overkill then? When should Data.Map be preferred over lists of tuples?
  • the Map was created from tuples of Strings – is it fine or should the key always be numeric in order for the balancing and lookups to work properly?
  • 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-15T20:09:41+00:00Added an answer on June 15, 2026 at 8:09 pm

    there were only around 2000 dates, therefore I didn’t care much about
    performance (you can see that I was using Strings everywhere); was
    using Data.Map an overkill then? When should Data.Map be preferred
    over lists of tuples?

    You should use data structures that fit your problem and performance/programming-time constraints, so using a Map was probably a good idea. Maybe in your case if your data was already ordered you could have done

    union [] _ = []
    union _ [] = []
    union xss@((dx,vx):xs) yss@((dy,vy):ys) = 
        case compare dx dy of
             EQ -> (dx, vx, vy) : union xs ys
             GT -> union xss ys
             LT -> union xs yss
    

    the Map was created from tuples of Strings – is it fine or should the
    key always be numeric in order for the balancing and lookups to work
    properly?

    No, if your code typechecks your Map will work properly (w/r/t the way you’ve defined Ord instance). But as C. A. McCann suggests, a trie may be more appropriate if your keys are lists, especially if there is much overlap between key prefixes (look at how the Ord instance on lists is implemented, and imagine the number of operations that have to take place to insert the keys “abcdx”, “abcdy”, and “abcdz” into a Map vs. a trie structure to convince yourself).

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

Sidebar

Related Questions

I recently needed to append some data to dynamically created LI elements . In
Recently I needed to compare a suggested pattern for IDisposable and object finalization with
I recently needed to create speech bubbles. To create the little triangular tip at
I'm developing my first java application using Eclipse. I've recently needed to adjust the
I was recently working on a project where I needed to convert a regular
I recently installed Oracle's VirtualBox on Ubuntu, due to the fact that I needed
I've recently encountered a problem designing a web page. I needed to have rounded
Recently one of our app servers went down, when it was rebooted the Python
I recently needed to get the integrity level of a process, and I found
I recently needed to serialize a datatable to JSON. Where I'm at we're still

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.