Multiple return and parallel assignement

yep. return 1,2 and return [1,2] work the same. Also when the
RHS or LHS of an assign is only single item and the other side
is multiple items you can think of that single item being
splatted (*):

a = 1,2 # *a = 1,2 # a = [1,2]
a,b = [1,2] # a,b = *[1,2] # a,b = 1,2

I've never quite understood why most languages inherently
support returning only a single value (Ruby uses an array to
make it look like multiple return values). Multiple return
values could have been passed back to the caller on the stack
just like arguments were passed to it. Maybe Ruby also uses an
array to implement multiple arguments. For Ruby, it really
doesn't matter too much since we have some niceties (splatting
and multiple assign) that give you the similar functionality as
true multiple return values. You can't differentiate between
something returning a single array and something returning
multiple values, but I don't think that is a big deal.

Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mailtour.html

···

--- James Britt <james_b@neurogami.com> wrote:

James Edward Gray II wrote:
> On May 20, 2005, at 8:05 AM, jean wrote:
>
>> What do you think about? (Perhaps I don't know enough Ruby
and there is
>> a way to do the same thing in Ruby :wink: )
>>
>> Thinking at Ruby I'd like that if I call mymethod defined
above with
>> only a variable it gets the first value:
>
>
> Probably not what you want, but:
>
> irb(main):001:0> def mymethod
> irb(main):002:1> return 1, 2
> irb(main):003:1> end
> => nil
> irb(main):004:0> one = mymethod.first
> => 1
>
> Hope that helps.

I think this helps point out that mymethod is not returning
multiple
values, but a single value (an Array object) that contains
multiple values.

Hi --

yep. return 1,2 and return [1,2] work the same. Also when the
RHS or LHS of an assign is only single item and the other side
is multiple items you can think of that single item being
splatted (*):

a = 1,2 # *a = 1,2 # a = [1,2]
a,b = [1,2] # a,b = *[1,2] # a,b = 1,2

Although:

   *a = *[1,2] # a == [1,2]

:slight_smile:

It's hard, I find, to come up with an exact description of what the
unarr?ay (unary unarray) operator does that fits every case. The
closest I've come is: *a = x means: a gets assigned that which, when
stripped of one level of array-ness, is x. Thus:

   *a = 1,2 # [1,2] stripped of is 1,2 so a is [1,2]
   *a = *[1,2] # a stripped of is [1,2] stripped of , so
               # a is [1,2]

   def x(*args); end
   x(1,2,3) # [1,2,3] stripped of is 1,2,3, so args is [1,2,3]
   x([1,2,3]) # [[1,2,3]] stripped of is [1,2,3], so args
              # is [[1,2,3]]

Then there's

   a = 1,2 # automatic arraying -- the opposite of *
   a = *[1,2] # un-arraying followed by automatic arraying :slight_smile:

or something like that.

David

···

On Sat, 21 May 2005, Eric Mahurin wrote:

--
David A. Black
dblack@wobblini.net

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though that the object may be a container for other objects, rather than assert that expressions may return an unknown number of objects

James

I also think it may be from the original mathematical idea of a "function', which only has 1 output value for a given set of inputs, which 3rd generation programming languages adopted as paradigm. With multiple return values, more in the mindset of a "relation", an n to m mapping

Ralph "PJPizza" Siegler

···

On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:

Eric Mahurin wrote:
>I've never quite understood why most languages inherently
>support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than assert
that expressions may return an unknown number of objects

James

David A. Black wrote:

Hi --

It's hard, I find, to come up with an exact description of what the
unarr?ay (unary unarray) operator does that fits every case. The
closest I've come is: *a = x means: a gets assigned that which, when
stripped of one level of array-ness, is x. Thus:

   *a = 1,2 # [1,2] stripped of is 1,2 so a is [1,2]
   *a = *[1,2] # a stripped of is [1,2] stripped of , so
               # a is [1,2]

   def x(*args); end
   x(1,2,3) # [1,2,3] stripped of is 1,2,3, so args is [1,2,3]
   x([1,2,3]) # [[1,2,3]] stripped of is [1,2,3], so args
              # is [[1,2,3]]

Then there's

   a = 1,2 # automatic arraying -- the opposite of *
   a = *[1,2] # un-arraying followed by automatic arraying :slight_smile:

or something like that.

David

Maybe a simpler way to look at it is *a = x means a = , or simply enclose
the rhs in an array before assignment.

Luca

···

On Sat, 21 May 2005, Eric Mahurin wrote:

And a simple rule of thumb to 'implement' it is: every time you remove a
* on one side of the =, add a to the other side.

*a = 1,2 --> a = [1,2]
*a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]

etc.

martin

···

David A. Black <dblack@wobblini.net> wrote:

It's hard, I find, to come up with an exact description of what the
unarr?ay (unary unarray) operator does that fits every case. The
closest I've come is: *a = x means: a gets assigned that which, when
stripped of one level of array-ness, is x. Thus:

Then you would prefer that a function/method only takes one object as
parameter, for conceptual elegance?

No, this is all coming from implementation simplification.

···

On 5/20/05, James Britt <james_b@neurogami.com> wrote:

Eric Mahurin wrote:
> I've never quite understood why most languages inherently
> support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than assert
that expressions may return an unknown number of objects

--
Guillaume Cottenceau - http://zarb.org/~gc/

Ralph "PJPizza" Siegler wrote:

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than
assert that expressions may return an unknown number of objects

James

I also think it may be from the original mathematical idea of a
"function', which only has 1 output value for a given set of inputs,

Mathematical functions are in no way limited to yielding a single output
value.

which 3rd generation programming languages adopted as paradigm. With
multiple return values, more in the mindset of a "relation", an n to
m mapping

That's what a mathematical function is basically. Don't let sin, cos,
max, min etc. fool you on this one. :slight_smile:

Kind regards

    robert

···

On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:

Hi --

···

On Sat, 21 May 2005, Luca Pireddu wrote:

David A. Black wrote:

Hi --

On Sat, 21 May 2005, Eric Mahurin wrote:

It's hard, I find, to come up with an exact description of what the
unarr?ay (unary unarray) operator does that fits every case. The
closest I've come is: *a = x means: a gets assigned that which, when
stripped of one level of array-ness, is x. Thus:

   *a = 1,2 # [1,2] stripped of is 1,2 so a is [1,2]
   *a = *[1,2] # a stripped of is [1,2] stripped of , so
               # a is [1,2]

   def x(*args); end
   x(1,2,3) # [1,2,3] stripped of is 1,2,3, so args is [1,2,3]
   x([1,2,3]) # [[1,2,3]] stripped of is [1,2,3], so args
              # is [[1,2,3]]

Then there's

   a = 1,2 # automatic arraying -- the opposite of *
   a = *[1,2] # un-arraying followed by automatic arraying :slight_smile:

or something like that.

David

Maybe a simpler way to look at it is *a = x means a = , or simply enclose
the rhs in an array before assignment.

I'm not sure how that's different from what I was saying above. Or
did you mean a simpler way to say it? :slight_smile:

David

--
David A. Black
dblack@wobblini.net

------------------------^
I think you dropped a '*':

*a = *[1,2] --> [a] = [*[1,2]] --> a = [1,2]

Is there a single good reference page that summarizes the rules for

     multiple assignment
     actual to formal argument assignment (for methods/lambdas)
     actual to formal argument assignment (for blocks)
     return expression to lhs assignment

I've seen it mentioned that the rules aren't quite identical for
all these scenarios.

Gary Wright

···

On May 21, 2005, at 5:35 PM, Martin DeMello wrote:

And a simple rule of thumb to 'implement' it is: every time you remove a
* on one side of the =, add a to the other side.

*a = 1,2 --> a = [1,2]
*a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]

Hi --

It's hard, I find, to come up with an exact description of what the
unarr?ay (unary unarray) operator does that fits every case. The
closest I've come is: *a = x means: a gets assigned that which, when
stripped of one level of array-ness, is x. Thus:

And a simple rule of thumb to 'implement' it is: every time you remove a
* on one side of the =, add a to the other side.

*a = 1,2 --> a = [1,2]
*a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]

Yes, though the illegality of [a] = x is what led me to phrase it in
such a verbose way :slight_smile:

However, using that formula:

   a = *[1,2] --> [a] = [1,2] --> a = 1,2

but a = 1,2 is the same as a = [1,2], which means that all of these
are the same:

   a = 1,2
   a = [1,2]
   a = *[1,2]

which makes sense given the automatic array-wrapping of 1,2 but is
worth noting since it looks kind of odd.

David

···

On Sun, 22 May 2005, Martin DeMello wrote:

David A. Black <dblack@wobblini.net> wrote:

--
David A. Black
dblack@wobblini.net

Guillaume Cottenceau wrote:

···

On 5/20/05, James Britt <james_b@neurogami.com> wrote:

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than assert
that expressions may return an unknown number of objects

Then you would prefer that a function/method only takes one object as
parameter, for conceptual elegance?

Maybe. A vector.

Or, um, a list.

James

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

David A. Black wrote:

Hi --

David A. Black wrote:

Hi --

It's hard, I find, to come up with an exact description of what the
unarr?ay (unary unarray) operator does that fits every case. The
closest I've come is: *a = x means: a gets assigned that which, when
stripped of one level of array-ness, is x. Thus:

   *a = 1,2 # [1,2] stripped of is 1,2 so a is [1,2]
   *a = *[1,2] # a stripped of is [1,2] stripped of , so
               # a is [1,2]

   def x(*args); end
   x(1,2,3) # [1,2,3] stripped of is 1,2,3, so args is [1,2,3]
   x([1,2,3]) # [[1,2,3]] stripped of is [1,2,3], so args
              # is [[1,2,3]]

Then there's

   a = 1,2 # automatic arraying -- the opposite of *
   a = *[1,2] # un-arraying followed by automatic arraying :slight_smile:

or something like that.

David

Maybe a simpler way to look at it is *a = x means a = , or simply
enclose the rhs in an array before assignment.

I'm not sure how that's different from what I was saying above. Or
did you mean a simpler way to say it? :slight_smile:

David

Hello. Yes, I wasn't trying to correct you. Just suggesting a simpler way of
saying the same thing :slight_smile:

Luca

···

On Sat, 21 May 2005, Luca Pireddu wrote:

On Sat, 21 May 2005, Eric Mahurin wrote:

Hi --

···

On Sun, 22 May 2005 gwtmp01@mac.com wrote:

On May 21, 2005, at 5:35 PM, Martin DeMello wrote:

And a simple rule of thumb to 'implement' it is: every time you remove a
* on one side of the =, add a to the other side.

*a = 1,2 --> a = [1,2]
*a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]

------------------------^
I think you dropped a '*':

*a = *[1,2] --> [a] = [*[1,2]] --> a = [1,2]

I think he meant to. See above: remove a * and add a . It's
happening on both sides here.

David

--
David A. Black
dblack@wobblini.net

Robert Klemme wrote:

Ralph "PJPizza" Siegler wrote:

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than
assert that expressions may return an unknown number of objects

James

I also think it may be from the original mathematical idea of a
"function', which only has 1 output value for a given set of inputs,

Mathematical functions are in no way limited to yielding a single output
value.

<pedant_mode>
But they are. A (mathematical) function f: M -> N from a set M to a
set N is usually defined as a subset of M x N such that for each
element m in M there exists exactly one element n in N such that
the pair (m, n) is in the subset; one then writes f(m) = n.
</pedant_mode>

which 3rd generation programming languages adopted as paradigm. With
multiple return values, more in the mindset of a "relation", an n to
m mapping

That's what a mathematical function is basically. Don't let sin, cos,
max, min etc. fool you on this one. :slight_smile:

Maybe you are thinking of vector valued functions, where the return
value is a vector? In that case, one still has only one return value
(the vector) which in turn contains other elements. This is very
similar to returning an array in ruby.

Regards,

Michael

···

On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:

--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com

James Britt wrote:

Guillaume Cottenceau wrote:

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than
assert that expressions may return an unknown number of objects

Then you would prefer that a function/method only takes one object as
parameter, for conceptual elegance?

Maybe. A vector.

Or, um, a list.

Vector implements List...
:wink:

    Java the Hutt

···

On 5/20/05, James Britt <james_b@neurogami.com> wrote:

Yep, my bad. Thanks for the correction to my non-correction.

Gary Wright

···

On May 21, 2005, at 6:06 PM, David A. Black wrote:

On Sun, 22 May 2005 gwtmp01@mac.com wrote:

On May 21, 2005, at 5:35 PM, Martin DeMello wrote:

*a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]

------------------------^
I think you dropped a '*':

*a = *[1,2] --> [a] = [*[1,2]] --> a = [1,2]

I think he meant to. See above: remove a * and add a . It's
happening on both sides here.

Michael Ulm wrote:

Robert Klemme wrote:

Ralph "PJPizza" Siegler wrote:

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object,
though that the object may be a container for other objects,
rather than assert that expressions may return an unknown number
of objects

James

I also think it may be from the original mathematical idea of a
"function', which only has 1 output value for a given set of inputs,

Mathematical functions are in no way limited to yielding a single
output value.

<pedant_mode>
But they are. A (mathematical) function f: M -> N from a set M to a
set N is usually defined as a subset of M x N such that for each
element m in M there exists exactly one element n in N such that
the pair (m, n) is in the subset; one then writes f(m) = n.
</pedant_mode>

So what? M can be A x B x C and N can be D x E - or whatever.

which 3rd generation programming languages adopted as paradigm.
With multiple return values, more in the mindset of a "relation",
an n to m mapping

That's what a mathematical function is basically. Don't let sin,
cos, max, min etc. fool you on this one. :slight_smile:

Maybe you are thinking of vector valued functions, where the return
value is a vector? In that case, one still has only one return value
(the vector) which in turn contains other elements. This is very
similar to returning an array in ruby.

With your reasoning there is also just a single input value for math
functions. So either functions have a single input and a single output
value, or multiple at both ends. But claiming that there can be multiple
input values but only a single output value is at least a bit dishonest
IMHO.

Kind regards

    robert

···

On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:

Robert Klemme wrote:

James Britt wrote:
...

Maybe. A vector.

Or, um, a list.

Vector implements List...
:wink:

Oh, I know.

But references to list processing are more provocative.

:slight_smile:

JGB

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

Robert Klemme wrote:

Michael Ulm wrote:

Robert Klemme wrote:

Ralph "PJPizza" Siegler wrote:

Eric Mahurin wrote:

I've never quite understood why most languages inherently
support returning only a single value

Conceptual elegance?

Seems cleaner to say that every expression returns an object,
though that the object may be a container for other objects,
rather than assert that expressions may return an unknown number
of objects

James

I also think it may be from the original mathematical idea of a
"function', which only has 1 output value for a given set of inputs,

Mathematical functions are in no way limited to yielding a single
output value.

<pedant_mode>
But they are. A (mathematical) function f: M -> N from a set M to a
set N is usually defined as a subset of M x N such that for each
element m in M there exists exactly one element n in N such that
the pair (m, n) is in the subset; one then writes f(m) = n.
</pedant_mode>

So what? M can be A x B x C and N can be D x E - or whatever.

which 3rd generation programming languages adopted as paradigm.
With multiple return values, more in the mindset of a "relation",
an n to m mapping

That's what a mathematical function is basically. Don't let sin,
cos, max, min etc. fool you on this one. :slight_smile:

Maybe you are thinking of vector valued functions, where the return
value is a vector? In that case, one still has only one return value
(the vector) which in turn contains other elements. This is very
similar to returning an array in ruby.

With your reasoning there is also just a single input value for math
functions. So either functions have a single input and a single output
value, or multiple at both ends. But claiming that there can be multiple
input values but only a single output value is at least a bit dishonest
IMHO.

Indeed, strictly speaking, a mathematical function can only have one
input value. Mathematically, the notation f(x, y) is just a shorthand
for f([x, y]). If Kernigan and Richie had paid more attention at
math class we would have declarations like

(float, float) transform (float, float)

in C, and the world would be a better place for it :slight_smile:

Michael

···

On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:

--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com