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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:03:36+00:00 2026-05-22T23:03:36+00:00

I come from SML background and feel quite comfortable with high-order functions. But I

  • 0

I come from SML background and feel quite comfortable with high-order functions. But I don’t really get the idea of list comprehension. Is there any situation where list comprehension is more suitable than high-order functions on List and vice versa?

I heard somewhere that list comprehension is slower than high-order functions, should I avoid to use it when writing performance-critical functions?

For the example’ sake, take a look at Projecting a list of lists efficiently in F# where @cfern’s answer contains two versions using list comprehension and high-order functions respectively:

let rec cartesian = function
  | [] -> [[]]
  | L::Ls -> [for C in cartesian Ls do yield! [for x in L do yield x::C]]

and:

let rec cartesian2 = function
  | [] -> [[]]
  | L::Ls -> cartesian2 Ls |> List.collect (fun C -> L |> List.map (fun x->x::C))
  • 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-22T23:03:36+00:00Added an answer on May 22, 2026 at 11:03 pm

    Choosing between comprehensions and higher-order functions is mostly a matter of style. I think that comprehensions are sometimes more readable, but that’s just a personal preference. Note that the cartesian function could be written more elegantly like this:

    let rec cartesian = function  
      | [] -> [[]]  
      | L::Ls -> 
         [ for C in cartesian Ls do for x in L do yield x::C ]
    

    The interesting case is when writing recursive functions. If you use sequences (and sequence comprehensions), they remove some unnecessary allocation of temporary lists and if you use yield! in a tail-call position, you can also avoid stack overflow exceptions:

    let rec nums n = 
      if n = 100000 then []
      else n::(nums (n+1))
    // throws StackOverflowException
    nums 0 
    
    let rec nums n = seq {
      if n < 100000 then
        yield n
        yield! nums (n+1) }
    // works just fine
    nums 0 |> List.ofSeq 
    

    This is quite an interesting pattern, because it cannot be written in the same way using lists. When using lists, you cannot return some element and then make a recursive call, because it corresponds to n::(nums ...), which is not tail-recursive.

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

Sidebar

Related Questions

I come from a C#/.NET 2.0 and WinForms background, but I'd really like to
I come from a Java/C++ OOP background and am trying to get into JavaScript
I come from a java background, but I'm working on a django-based website. To
I come from a webforms background, and at this point I'm quite frustrated with
I come from a Java background, but I learned C++ after and have been
I come from a java background. But I would like a cross-platform perspective on
I come from python background and am learning ruby. IPython is really awesome. I
I come from c# background where immutable is achieved with public get ,private set
I come from a C# background but we have an app that is written
I come from the Java world, so to me it's all object.foo() , but

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.