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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:30:36+00:00 2026-05-31T05:30:36+00:00

I have a csv file with the following structure : The first line is

  • 0

I have a csv file with the following structure :

  1. The first line is a header row
  2. The remaining lines are data lines,
    each with the same number of commas, so we can think of the data in
    terms of columns

I have written a little script to go through each line of the file and return a sequence of tuples containing the column header and the length of the largest string of data in that column :

let getColumnInfo (fileName:string) =
    let delimiter = ','

    let readLinesIntoColumns (sr:StreamReader) = seq { 
        while not sr.EndOfStream do     
            yield sr.ReadLine().Split(delimiter) |> Seq.map (fun c -> c.Length )
    }

    use sr = new StreamReader(fileName)     
    let headers = sr.ReadLine().Split(delimiter) 
    let columnSizes =
        let initial = Seq.map ( fun h -> 0 ) headers
        let toMaxColLengths (accumulator:seq<int>) (line:seq<int>)  = 
             let chooseBigger a b = if a > b then a else b
             Seq.map2 chooseBigger accumulator line
        readLinesIntoColumns sr |> Seq.fold toMaxColLengths initial
    Seq.zip headers columnSizes;

This works fine on a small file. However when it trys to process a large file (> 75 Mb) it blows fsi with a StackOverflow exception. If I remove the line

Seq.map2 chooseBigger accumulator line

the program completes.

Now, my question is this : why is F# using up the stack? My understanding of sequences in F# is that the entire sequence is not held in memory, only the elements that are being processed. Therefore I expected that the lines that had already been processed would not remain on the stack. Where is my misunderstanding?

  • 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-31T05:30:37+00:00Added an answer on May 31, 2026 at 5:30 am

    I think this is a good question. Here’s a simpler repro:

    let test n =
        [for i in 1 .. n -> Seq.empty]
        |> List.fold (Seq.map2 max) Seq.empty
        |> Seq.iter ignore
    

    test creats a sequence of empty sequences, calculates the max by rows, and then iterates over the resulting (empty) sequence. You’ll find that with a high value of n this will cause a stack overflow, even though there aren’t any values to iterate over at all!

    It’s a bit tricky to explain why, but here’s a stab at it. The problem is that as you fold over the sequences, Seq.map2 is returning a new sequence which defers its work until it’s enumerated. Thus, when you try to iterate through the resulting sequence, you end up calling back into a chain of computations n layers deep.

    As Daniel explains, you can escape this by evaluating the resulting sequence eagerly (e.g. by converting it to a list).

    EDIT

    Here’s an attempt to further explain what’s going wrong. When you call Seq.map2 max s1 s2, neither s1 nor s2 is actually enumerated; you get a new sequence which, when enumerated, will enumerate both of them and compare the yielded values. Thus, if we do something like the following:

    let s0 = Seq.empty
    let s1 = Seq.map2 max Seq.emtpy s0
    let s2 = Seq.map2 max Seq.emtpy s1
    let s3 = Seq.map2 max Seq.emtpy s2
    let s4 = Seq.map2 max Seq.emtpy s3
    let s5 = Seq.map2 max Seq.emtpy s4
    ...
    

    Then the call to Seq.map2 always returns immediately and uses constant stack space. However, enumerating s5 requires enumerating s4, which requires enumerating s3, etc. This means that enumerating s99999 will build up a huge call stack that looks sort of like:

    ...
    (s99996's enumerator).MoveNext()
    (s99997's enumerator).MoveNext()
    (s99998's enumerator).MoveNext()
    (s99999's enumerator).MoveNext()
    

    and we’ll get a stack overflow.

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

Sidebar

Related Questions

I want the following functionality using php I have a csv file. Each file
I have a csv file with following sample data. o-option(alphabetical) v-value(numerical) number1,o1,v1,o2,v2,o3,v3,o4,v4,o5,v5,o6,v6 number2,o1,v11,o2,v22,o3,v33,o44,v44,o5,v55,o6,v66 and
I have a CSV file in the following format: id,code,date,address,complete_url Example data from the
I have a simple text file containing some CSV with the following structure: @Parent1_Field1,
I have csv file with following structure: A BA0011 U206 NAME 0000000000000149.00 000000.00 0000000000000118.93
I have a csv file which has a word for each row like this
I have text in the form of separate lines, where each line has CSV-like
I have the following admin action below that exports data to a CSV file,
I have a particular csv file with the following data: uid Business Area employeeNumber
I have the following line in a CSV file that's giving me issues when

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.