List comprehension vs high-order functions in F#

后端 未结 3 529
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 02:22

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:14

    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.

提交回复
热议问题