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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:46:31+00:00 2026-05-25T06:46:31+00:00

I am currently facing the problem of having to make my calculations based on

  • 0

I am currently facing the problem of having to make my calculations based on the length of a given list. Having to iterate over all the elements of the list to know its size is a big performance penalty as I’m using rather big lists.

What are the suggested approaches to the problem?

I guess I could always carry a size value together with the list so I know beforehand its size without having to compute it at the call site but that seems a brittle approach. I could also define a own type of list where each node has as property its the lists’ size but then I’d lose the leverage provided by my programming language’s libraries for standard lists.

How do you guys handle this on your daily routine?

I am currently using F#. I am aware I can use .NET’s mutable (array) lists, which would solve the problem. I am way more interested, though, in the purely immutable functional approach.

  • 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-25T06:46:32+00:00Added an answer on May 25, 2026 at 6:46 am

    The built-in F# list type doesn’t have any caching of the length and there is no way to add that in some clever way, so you’ll need to define your own type. I think that writing a wrapper for the existing F# list type is probably the best option.

    This way, you can avoid explicit conversions – when you wrap the list, it will not actually copy it (as in svick’s implementation), but the wrapper can easily cache the Length property:

    open System.Collections
    
    type LengthList<'T>(list:list<'T>) =
      let length = lazy list.Length
      member x.Length = length.Value
      member x.List = list
      interface IEnumerable with
        member x.GetEnumerator() = (list :> IEnumerable).GetEnumerator()
      interface seq<'T> with  //'
        member x.GetEnumerator() = (list :> seq<_>).GetEnumerator()
    
    [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
    module LengthList =
      let ofList l = LengthList<_>(l)
      let ofSeq s = LengthList<_>(List.ofSeq s)
      let toList (l:LengthList<_>) = l.List
      let length (l:LengthList<_>) = l.Length
    

    The best way to work with the wrapper is to use LengthList.ofList for creating LengthList from a standard F# list and to use LengthList.toList (or just the List) property before using any functions from the standard List module.

    However, it depends on the complexity of your code – if you only need length in a couple of places, then it may be easier to keep it separately and use a tuple list<'T> * int.

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

Sidebar

Related Questions

I'm currently facing new problem with operators. Using following code, I want to make
i'm facing a problem, currently i'm having a update panel in my master page
I am facing a rather strange problem with make. My make file contains: all:
Im currently facing the problem that when i try to set focus on some
I'm currently facing a problem which I can not resolve and I really don't
I'm currently facing this problem with ruby on rails 3, devise and facebook oauth.
I'm currently facing a weird problem while executing a command from my bash script.
I'm developing android application with phonegap . Currently I'm facing one problem with inputbox
I am currently working on a Wordpress blog installation. Problem i am facing is
I am currently facing a very disturbing problem: interface IStateSpace<Position, Value> where Position :

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.