Why don't purely functional languages use reference counting?

前端 未结 6 1202
忘了有多久
忘了有多久 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:57

    Your question is based on a faulty assumption. It's perfectly possible to have circular references and immutable data. Consider the following C# example which uses immutable data to create a circular reference.

    class Node { 
      public readonly Node other;
      public Node() { 
        other = new Node(this);
      }
      public Node(Node node) {
        other = node;
      }
    }
    

    This type of trick can be done in many functional languages and hence any collection mechanism must deal with the possibility of circular references. I'm not saying a ref counting mechanism is impossible with a circular reference, just that it must be dealt with.

    Edit by ephemient

    In response to the comment... this is trivial in Haskell

    data Node a = Node { other :: Node a }
    recursiveNode = Node { other = recursiveNode }
    

    and barely any more effort in SML.

    datatype 'a node = NODE of unit -> 'a node
    val recursiveNode : unit node =
        let fun mkRecursiveNode () = NODE mkRecursiveNode
        in mkRecursiveNode () end
    

    No mutation required.

提交回复
热议问题