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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:34:07+00:00 2026-05-27T13:34:07+00:00

I’m currently working with CSV files where I’m parsing them into a [[String]] The

  • 0

I’m currently working with CSV files where I’m parsing them into a [[String]]
The first [String] in that array is a header file eg:

["Code","Address","Town"]

and the rest are arrays of information

["ABA","12,east road", "London"]

I would like to create a query system where input and the result will look something like this

>count "Town"="*London*" @1="A*"
14 rows

The column name could be put in as a string Or as a @ with the index of the column
I have a case switch to recognise the first word input since Im going to expand my CSV reader for different functions.
When It sees the word count it will go to a function that will return a count of rows. Im not sure how to start doing the parsing of the query.
At first I thought I might split the resulting string after the word count into a list of strings with each query, perform one and use the list that satisfied this query to be checked again for the next, leaving with a list for which all queries are satisfied, then counting amount of entries and returning them. There would be a case switch also to recognise if the first input is a string or an @ symbol.
The * are used to represent zero or any character following the word.
I am not sure how to start implementing this or if im missing a problem I might encounter with my solution. I will be great full for any help with starting me off. Im not very advanced with Haskell(since Im just starting), so I would also appreciate keeping it simple. Thank you

  • 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-27T13:34:08+00:00Added an answer on May 27, 2026 at 1:34 pm

    Here’s one possible approach.

    First, let us move away from your list-of-list-of-string representation a bit and let us represents records as key/value pairs, such that a database is just a list of records:

    type Field  = (String, String) -- key, value                                    
    type Record = [Field]
    type Db     = [Record]
    

    Reading in CSV data in your representation then becomes:

    type Csv = [[String]]
    
    fromCsv :: Csv -> Db
    fromCsv []         = []
    fromCsv (ks : vss) = map (zip ks) vss
    

    Now, let us talk about queries. In your setting, a query is essentially a list of filters, where each filter identifies a field and matches a set of values:

    type Query  = [Filter]
    type Filter = (Selector, ValueFilter)
    

    Fields are selected either by name or by a one-based (!) index:

    data Selector = FieldName String | FieldIndex Int
    

    Values are matched by applying a sequence of simple parsers, where a parser either recognises a single character or otherwise a sequence of zero or more arbitrary characters:

    type ValueFilter = [Parser]
    data Parser      = Char Char | Wildcard
    

    Parsing can be implemented using the list-of-successes method, where each success denotes the remaining input, i.e., the part of the input that was not consumed by the parser. An empty list of remaining inputs denotes failure. (So, note the difference between [] and [[]] in the produced results in the cases below.)

    parse :: Parser -> String -> [String]
    parse (Char c) (c' : cs') | c == c' = [cs']
    parse Wildcard []                   = [[]]
    parse Wildcard cs@(_ : cs')         = cs : parse Wildcard cs'
    parse _ _                           = []
    

    Filtering values then develops into backtracking:

    filterValue :: ValueFilter -> String -> Bool
    filterValue ps cs = any null (go ps cs)
      where
        go [] cs       = [cs]
        go (p : ps) cs = concatMap (go ps) (parse p cs)
    

    Value selection is straightforward:

    select :: Selector -> Record -> Maybe String
    select (FieldName s) r                           = lookup s r
    select (FieldIndex n) r | n > 0 && n <= length r = Just (snd (r !! (n - 1)))
                            | otherwise              = Nothing
    

    Applying a record filter now amounts to constructing a predicate over records:

    apply :: Filter -> Record -> Bool
    apply (s, vf) r = case select s r of
      Nothing -> False
      Just v  -> filterValue vf v
    

    Finally, for executing a complete query, we have

    exec :: Query -> Db -> [Record]
    exec = (flip . foldl . flip) (filter . apply)
    

    (I leave the parsing of queries themselves as an exercise:

    readQuery :: String -> Maybe Query
    readQuery = ...
    

    but I recommend using a parser-combinator library such as parsec or uulib.)

    Now, let’s test. First, we introduce a small database in CSV-format:

    csv :: Csv
    csv =
      [ ["Name" , "City"      ]
         -------  ------------                                                      
      , ["Will" , "London"    ]
      , ["John" , "London"    ]
      , ["Chris", "Manchester"]
      , ["Colin", "Liverpool" ]
      , ["Nick" , "London"    ]
      ]
    

    Then, we construct a simple query:

    -- "Name"="*i*" @2="London"                                                     
    query :: Query
    query =
      [ (FieldName "Name", [Wildcard, Char 'i', Wildcard])
      , (FieldIndex 2,
          [Char 'L', Char 'o', Char 'n', Char 'd', Char 'o', Char 'n'])
      ]
    

    And, indeed, running our query against the database yields:

    > exec query (fromCsv csv)
    [[("Name","Will"),("City","London")],[("Name","Nick"),("City","London")]]
    

    Or, if you are only after counting the results of your query:

    > length $ exec query (fromCsv csv)
    2
    

    Of course, this is just one approach and for sure one can think of many alternatives. A nice aspect of breaking the problem down in small functions, as we have done above, is that you can easily test and experiment with small chunks of the solution in isolation.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.