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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:00:42+00:00 2026-05-23T04:00:42+00:00

I’m writing some code that needs to frequently append to the end of a

  • 0

I’m writing some code that needs to frequently append to the end of a list. I know that using “++” is inefficient. So instead I build up the list backwards by appending to the head, and then reverse it when I’m done. I gather that this a common beginner tactic.

I would rather build it up in the correct order to begin with – but that means switching to a new data structure. I’m considering using Data.Sequence or Data.DList for my container. My list consists of strict int pairs, and I don’t need random access to it. What are the relative merits of Data.Sequence and Data.DList, and are there other containers I should consider?

  • 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-23T04:00:42+00:00Added an answer on May 23, 2026 at 4:00 am

    Whether to use Data.Sequence or DList depends on how you are going to be using the resulting list. DList is great when you are building up a sequence, say in a Writer computation, to convert to a list at the end and use it. However, if you need to use the intermediate results, like, say:

    f (foo ++ bar)
    + f (foo ++ bar ++ baz)
    + f (foo ++ bar ++ baz ++ quux)
    

    then DList is pretty bad, because it needs to recompute the spine each time. Data.Sequence is a better choice in this situation. Data.Sequence is also better if you need to remove elements from the sequence.

    But maybe you don’t even need to make this decision. Reversing lists at the end of a computation is common in strict functional languages like ML and Scheme, but not in Haskell. Take, for example, these two ways of writing map:

    map_i f xs = reverse $ go [] xs
        where
        go accum [] = accum
        go accum (x:xs) = go (f x : accum) xs
    
    map_ii f [] = []
    map_ii f (x:xs) = f x : map_ii f xs
    

    In a strict language, map_ii would be horrible because it uses linear stack space, whereas map_i is tail recursive. But because Haskell is lazy, map_i is the inefficient one. map_ii can consume one element of the input and yield one element of the output, whereas map_i consumes the whole input before yielding any output.

    Tail recursion isn’t the holy grail of efficient implementation in Haskell. When producing a data structure like a list, you actually want to be co-recursive; that is, make the recursive call underneath an application of a constructor (eg. f x : map_ii f xs above).

    So if you find yourself reversing after a tail-recursive function, see if you can factor the whole lot into a corecursive function.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.