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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:51:36+00:00 2026-05-13T20:51:36+00:00

I’m trying to create a piece of code but cannot get it working. The

  • 0

I’m trying to create a piece of code but cannot get it working. The simplest example I can think of is parsing some CSV file.
Suppose we have a CVS file, but the data is organized in some kind of hierarchy in it. Like this:

Section1;
        ;Section1.1
        ;Section1.2
        ;Section1.3
Section2;
        ;Section2.1
        ;Section2.2
        ;Section2.3
        ;Section2.4

etc.

I did this:

let input = 
"a;
;a1
;a2
;a3
b;
;b1
;b2
;b3
;b4
;b5
c;
;c1"

let lines = input.Split('\n') 
let data = lines |> Array.map (fun l -> l.Split(';'))

let sections = 
  data 
  |> Array.mapi (fun i l -> (i, l.[0])) 
  |> Array.filter (fun (i, s) -> s <> "")

and I got

val sections : (int * string) [] = [|(0, "a"); (4, "b"); (10, "c")|]

Now I’d like to create a list of line index ranges for each section, something like this:

[|(1, 3, "a"); (5, 9, "b"); (11, 11, "c")|]

with the first number being a starting line index of the subsection range and the second – the ending line index. How do I do that? I was thinking about using fold function, but couldn’t create anything.

  • 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-13T20:51:37+00:00Added an answer on May 13, 2026 at 8:51 pm

    As far as I know, there is no easy way to do this, but it is definitely a good way to practice functional programming skills. If you used some hierarchical representation of data (e.g. XML or JSON), the situation would be a lot easier, because you wouldn’t have to transform the data structure from linear (e.g. list/array) to hierarchical (in this case, a list of lists).

    Anyway, a good way to approach the problem is to realize that you need to do some more general operation with the data – you need to group adjacent elements of the array, starting a new group when you find an line with a value in the first column.

    I’ll start by adding a line number to the array and then convert it to list (which is usually easier to work with in F#):

    let data = lines |> Array.mapi (fun i l -> 
      i, l.Split(';')) |> List.ofSeq
    

    Now, we can write a reusable function that groups adjacent elements of a list and starts a new group each time the specified predicate f returns true:

    let adjacentGroups f list =
      // Utility function that accumulates the elements of the current 
      // group in 'current' and stores all groups in 'all'. The parameter
      // 'list' is the remainder of the list to be processed
      let rec adjacentGroupsUtil current all list =
        match list with
        // Finished processing - return all groups
        | [] -> List.rev (current::all) 
        // Start a new group, add current to the list
        | x::xs when f(x) -> 
          adjacentGroupsUtil [x] (current::all) xs
        // Add element to the current group
        | x::xs ->
          adjacentGroupsUtil (x::current) all xs
    
      // Call utility function, drop all empty groups and
      // reverse elements of each group (because they are
      // collected in a reversed order)
      adjacentGroupsUtil [] [] list
        |> List.filter (fun l -> l <> [])
        |> List.map List.rev
    

    Now, implementing your specific algorithm is relatively easy. We first need to group the elements, starting a new group each time the first column has some value:

    let groups = data |> adjacentGroups (fun (ln, cells) -> cells.[0] <> "")
    

    In the second step, we need to do some processing for each group. We take its first element (and pick the title of the group) and then find the minimal and maximal line number among the remaining elements:

    groups |> List.map (fun ((_, firstCols)::lines) ->
      let lineNums = lines |> List.map fst
      firstCols.[0], List.min lineNums, List.max lineNums )
    

    Note that the pattern matching in the lambda function will give a warning, but we can safely ignore that because the group will always be non-empty.

    Summary: This answer shows that if you want to write elegant code, you can implement your reusable higher order function (such as adjacentGroups), because not everything is available in the F# core libraries. If you use functional lists, you can implement it using recursion (for arrays, you’d use imperative programming as in the answer by gradbot). Once you have a good set of reusable functions, most of the problems are easy :-).

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

Sidebar

Ask A Question

Stats

  • Questions 412k
  • Answers 412k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can write your own extension method to do something… May 15, 2026 at 8:00 am
  • Editorial Team
    Editorial Team added an answer I think the closest you can get to what you… May 15, 2026 at 8:00 am
  • Editorial Team
    Editorial Team added an answer SELECT name, COUNT(name)/(SELECT COUNT(1) FROM names) FROM names GROUP BY… May 15, 2026 at 8:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.