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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:03:03+00:00 2026-05-18T11:03:03+00:00

In Haskell, when using : repeatedly to create a sequence, iterating through the result

  • 0

In Haskell, when using : repeatedly to create a sequence, iterating through the result takes O(N) time.

1:2:3:4:[]

If I try to do something similar in LINQ, I can use Concat(), but iterating through the result takes O(N²) time, because when I call MoveNext() on the resulting enumerator for n-th time, it has to go through n layers of Concat()s.

new[] { 1 }.Concat(new[] { 2 }).Concat(new[] { 3 }).Concat(new[] { 4 })

or

new[] { 1 }.Concat(new[] { 2 }.Concat(new[] { 3 }.Concat(new[] { 4 })))

How can I do this, so that it’s fast (i.e. linear) and functionally “clean” (i.e. without using List<T>)? Maybe by writing IConcatenable<T> with its own overload of Concat()? Or am I wrong that using Concat() this way is quadratic?

  • 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-18T11:03:03+00:00Added an answer on May 18, 2026 at 11:03 am

    The LINQ Concat() operation is in no way equivalent to the functional cons operation. They are very different. For every cons operation, a “new” list is created. This is actually fast since the data structures in play are designed specifically for this use. For every Concat operation, a new iterator is created and isn’t really meant for this kind of use. To illustrate what is done each time, consider the following shorter example:

    1:2:3:[]
    

    The functional operation consists of a few steps to evaluate. 3:[] results in the temporary list [3], then 2:[3] results in the list [2,3] and 1:[2,3]. That’s 3 simple steps just to create the list. To “iterate” through it, will require some recursion and matching (or whatever the Haskell equivalent is). That will take somewhere in the vicinity of 3 or 4 more complex steps (one for each list segment).

    new[] { 1 }.Concat(new[] { 2 }).Concat(new[] { 3 });
    

    The LINQ operation consists of a few steps too to evaluate. new[] { 1 }.Concat(new[] { 2 }) yields a new iterator iterating through the sequence [[1]+[2]], and the last yields a new iterator iterating through the sequence [[[1]+[2]]+[3]]. That took two simple steps just to create the iterator, but you should notice how complex the sequence actually is represented. 5 iterators are used here, one for each array (3) and one for each concatenated pair (2). I won’t go through all the steps of the iteration but this does many more function calls and property accesses (as required by each iterator).

    new[] { 1 }.Concat(new[] { 2 }.Concat(new[] { 3 }))
    

    The LINQ operation that looks more structurally equivalent again takes the same amount of steps to create, yielding [[1]+[[2]+[3]]] with the same amount of iterators and just as complex iterating though it.

    You might be thinking the functional version should be more complex because we need some recursive functions. Well it’s not because that’s the way things are done in functional languages and they are optimized for this use. They have the advantage of using immutable sequences that could be reused in other composed lists. Generating complex sequences using LINQ in this way isn’t really what it was designed to do. There are no optimizations, by the language (some by the JIT but that’s it) and it’s clearly not how you’d want to iterate through a sequence. You hit it right on the head IMHO on why it is complex.


    I think the better approach in an attempt at better performance is to create linked lists to represent the concatenated sequence. You could either use the LinkedList<T> class, create your own data structures reminiscent of a functional list’s or even better, use F#’s FSharpList<T>. Then extend with extension methods and other supporting classes.

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

Sidebar

Related Questions

When messing around with Haskell using GHC, I can use various meta-commands like :i
Is there any XML-(de)serializer for Haskell using Data / Typeable with functions similar to
While monads are represented in Haskell using the bind and return functions, they can
I came across the following definition as I try to learn Haskell using a
What is the best way to interact with a database using Haskell? I'm accustomed
How would I even go about forking a child process using Haskell in the
I know a Haskell module name, but I can't figure out in what package
I'm writing an audio program in Haskell using Portaudio. I have a function that
I want to implement a simple SceneGraph in Haskell using Data.Tree consisting of Transform
I am using Haskell to solve problem 99 in euler project, where I must

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.