List comprehension vs high-order functions in F#

后端 未结 3 1253
星月不相逢
星月不相逢 2021-02-09 02:31

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 co

3条回答
  •  难免孤独
    2021-02-09 03:13

    Looking at the generated code in ILSpy, you can see that list comprehensions are compiled to state machines (like methods using yield return in C#), then passed to something like List.ofSeq. Higher-order functions, on the other hand, are hand-coded, and frequently use mutable state or other imperative constructs to be as efficient as possible. As is often the case, the general-purpose mechanism is more expensive.

    So, to answer your question, if performance is critical there is usually a higher-order function specific to your problem that should be preferred.

提交回复
热议问题