3 ms·
Common Lisp has multiple return values and it's quite useful: (let x (f 12) ...) ; bind x to the first value returned by (f 12) and silently drop the other
by ced 13y ago
Common Lisp has multiple return values and it's quite useful:
(let x (f 12) ...) ; bind x to the first value returned by (f 12) and silently drop the others
(let (x y) (f 12) ...) ; bind x to the first value and y to the second.
That way, I can return more information without breaking existing code. That point is really important: in Python I can gracefully add an extra argument, from
def foo(x):
...
to
def foo(x, y=None):
...
but I can't return an extra value without changing (and uglifying!) all calling code to handle the tuple.
The answer to the OP is probably that all computer languages derive from Fortran, and Fortran derived from maths, so it chose f(x, y) as the notation for function application. With that notation, there's no elegant way of handling more return values, and language designers might feel that the extra complexity/ugliness isn't worth it. That, and intellectual inertia.
- gjm11 13y agoTechnical correction: not (let (x y) (f 12) ...) but (multiple-value-bind (x y) (f 12) ...) (There are also multiple-value-call and multiple-value-list, which are more powerful than multiple-value-bind because they let you get all the values returned without knowing in advance how many there are, but usually less convenient.)
- informatimago 13y agoThose let forms ares not Common Lisp. However, they could be mplemented by a user with a lisp macro.
- deleted 13y ago[deleted]
- ubasu 13y agoAllow me to bring up Lua, which has a nice way of handling multiple return values: http://www.lua.org/pil/5.1.html http://www.lua.org/pil/5.1.html
- draegtun 13y ago>Common Lisp has multiple return values and it's quite useful: Ditto for Perl: sub f { 1..$_[0] } my ($x) = f(12); # $x = first value, rest are dropped ... my ($x, $y) = f(12); # $x & $y have first two values, rest are dropped ... my ($x, $y, @rest) = f(12); # $x=1, $y=2 & @rest=3..12