Why don't purely functional languages use reference counting?

前端 未结 6 1210
忘了有多久
忘了有多久 2021-02-02 06:27

In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional la

6条回答
  •  醉酒成梦
    2021-02-02 06:50

    There are a few things, I think.

    • There are cycles: "let rec" in many languages does allow "circular" structures to be created. Apart from this, immutability does usually imply no cycles, but this breaks the rule.
    • Ref-counts are bad at lists: I don't know that reference-counted collection works well with e.g. long singly-linked-list structures you often find in FP (e.g. slow, need to ensure tail-recursive, ...)
    • Other strategies have benefits: As you allude to, other GC strategies are still usually better for memory locality

    (Once upon a time I think I maybe really 'knew' this, but now I am trying to remember/speculate, so don't take this as any authority.)

提交回复
热议问题