Arc Forumnew | comments | leaders | submitlogin
4 points by nex3 5921 days ago | link | parent

Lisps pass some objects (lists, hash tables) by reference, but others (integers, symbols) by value. In languages that offer it like C++, explicit pass-by-reference is useful because A) these languages don't deal with objects as references by default and B) to pass extra information to the caller of the function (e.g. having a boolean reference that's set to true if some condition is met).

However, A is a non-issue in Lisps and B is trivially solvable by returning a pair, so I don't see how pass-by-reference could be useful.



2 points by rkts 5920 days ago | link

Consider deleting a node from a binary tree. You want a function that looks at a node and, if it's a match, unlinks it from its parent. Passing by reference allows you to do this cleanly. The alternative is to peek ahead at each subnode (messy) or to pass information up the call stack (inefficient, as the function can no longer be tail-recursive).

I'm not advocating C++ references, which I think are too implicit. If a function call foo(x) can change the value of x, there needs to be some visual indication of this. I'd prefer something like plain C pointers, with which you can pass the address of an object: foo(&x).

Of course an alternative is store objects wrapped in containers, and this isn't too bad a solution.

  (def ref (x) (obj contents x))
  (mac deref (x) `(,x 'contents))
But of course this is inefficient. If anything, this argues for adding arrays to Arc.

-----

1 point by EliAndrewC 5920 days ago | link

> However, A is a non-issue in Lisps and B is trivially solvable by returning a pair, so I don't see how pass-by-reference could be useful.

Is there an Arc equivalent to Common Lisp's multiple-value-bind macro? Because it's very awkward to have to say

    (let values (f x)
        (with (a (values 0)
               b (values 1))
            (whatever ....

-----

3 points by rkts 5920 days ago | link

Yes:

  (let (a b) (f x) ...)
although technically this is a destructuring-bind, not a multiple-value-bind.

-----