[Prev][Next][Index][Thread]

Re: small Q



Scott McKay <swm@mediaone.net> wrote:
> oodl@my-deja.com wrote in message <8mmvgo$enu$1@nnrp1.deja.com>...
> >In article <Gsyj5.3194$pu4.263441@typhoon.ne.mediaone.net>,
> >  "Scott McKay" <swm@mediaone.net> wrote:
> >>  Multiple return values
> >> is one of those things that, once you use them, you can't imagine why
> >> all languages don't have them.
> >
> >Yes, imagine if functions in programming languages could only *take* one
> >parameter.  That would be lame.  Being able to return only one value
> >from a function is no different:  It forces one to resort to structures,
> >collections, or global variables.... all of which have a negative
> >influence on organization and/or clarity of ones source code.
> 
> FWIW, I think that ML, e.g., only allows you to pass a single
> parameter.  Passing multiple parameters is syntactic sugar
> for a currying sort of operation.
> 
> My memory could be wrong...

This is right, but not quite the whole story. ML's functions all
accept one argument and return one value, like lambda in the lambda
calculus. So by default, multiple arguments curry a definition like

  fun foo x y = x + y;

defines a curried function of type int -> int -> int. This is
equivalent to the Dylan

  define method foo (x)
    method(y) x + y end
  end method;

However, ML supports pattern matching, so you can also define
a function that takes a tuple as an argument:

  fun bar (x,y) = x + y;

which has type fn: (int * int) -> int. This is also how ML
handles multiple return values:

  fun baz x y = (x+y, x*y);

has type int -> int -> (int * int). This is different from Scheme's
values/call-with-values or Dylan's values/let, in that at the semantic
level a tuple is being returned in ML rather than a continuation that
takes multiple arguments is constructed as in Scheme or Dylan.

At the implementation level, both languages optimize the hell out of
this, so it's a bit of a moot point.


Neel



References: