Aggregate types 
Author Message
 Aggregate types

Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
algorithms, it would sometimes be useful to return an ordered pair (or
n-tuple) of types from a function. Such as this trivial example:

(int, int) order(int x, int y)
{
  if (x<=y)
    return (x, y);
  else
    return (y, x);

Quote:
}

Then it could be used such as:

int main(void)
{
  int a=100;
  int b=50;
  (a, b)=order(a, b);
  return 0;

Quote:
}

As the scalar type names are always given in the aggregate types, I
don't think this introduces any ambiguity. Is it just because of
difficulty of implementation this kind of thing isn't possible in C?

<OT>
Are there any languages (preferably similar to C) where this is
possible?
</OT>

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."  
   - Bill Gates



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:
>Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
>algorithms, it would sometimes be useful to return an ordered pair (or
>n-tuple) of types from a function. Such as this trivial example:

But C does have aggregate types!

n869, 6.2.5 (Types)

[...]

       [#21]  Integer  and  floating  types are collectively called
       arithmetic types. Arithmetic types  and  pointer  types  are
       collectively  called scalar types. Array and structure types
       are collectively called aggregate types.34)

[snip]



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types


:>Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
:>algorithms, it would sometimes be useful to return an ordered pair (or
:>n-tuple) of types from a function. Such as this trivial example:

: But C does have aggregate types!

: n869, 6.2.5 (Types)

: [...]

:        [#21]  Integer  and  floating  types are collectively called
:        arithmetic types. Arithmetic types  and  pointer  types  are
:        collectively  called scalar types. Array and structure types
:        are collectively called aggregate types.34)

: [snip]

OK, so I chose the name poorly. But why doesn't C have *the aggregate
types that I was meaning*?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"To err is human. To really louse things up takes a computer."
   - Anon



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types
Joona,

Quote:



>>> Why doesn't C use aggregate types? (Or type aggregates.)
>>> In mathematical algorithms, it would sometimes be useful to
>>> return an ordered pair (or n-tuple) of types from a function.
>>> Such as this trivial example:
>> But C does have aggregate types!
> OK, so I chose the name poorly. But why doesn't C have *the
> aggregate types that I was meaning*?

  C is a low level programming language and, generally, does not need
high level types like those required by mathematical algorithms.
Nevertheless, you can build a higher level writing your own mathematical
library or using an existing one or use tools like MatLab.

        Best regards,
                Marco



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:

> Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
> algorithms, it would sometimes be useful to return an ordered pair (or
> n-tuple) of types from a function. Such as this trivial example:

> (int, int) order(int x, int y)
> {
>   if (x<=y)
>     return (x, y);
>   else
>     return (y, x);
> }

> Then it could be used such as:

> int main(void)
> {
>   int a=100;
>   int b=50;
>   (a, b)=order(a, b);
>   return 0;
> }

What about

typedef struct xy
{
        int x;
        int y;

Quote:
}xy_order;

xy_order Order(xy_order x_y)
{
        xy_order tmp;
        if (x_y.x<=x_y.y)
        {
                tmp.x=x_y.y;
                tmp.y=x_y.x;
                return tmp;
        }
        else
                return x_y;

Quote:
}

int main(void)
{
        xy_order tst={100,200};
        tst=Order(tst);
        return 0;

- Show quoted text -

Quote:
}
> As the scalar type names are always given in the aggregate types, I
> don't think this introduces any ambiguity. Is it just because of
> difficulty of implementation this kind of thing isn't possible in C?

> <OT>
> Are there any languages (preferably similar to C) where this is
> possible?
> </OT>

> --

> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
> | http://www.helsinki.fi/~palaste       W++ B OP+                     |
> \----------------------------------------- Finland rules! ------------/

> "The obvious mathematical breakthrough would be development of an easy way to
> factor large prime numbers."
>    - Bill Gates

Kind regards
Robert


Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:



> :>Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
> :>algorithms, it would sometimes be useful to return an ordered pair (or
> :>n-tuple) of types from a function. Such as this trivial example:

> : But C does have aggregate types!

> : n869, 6.2.5 (Types)

> : [...]

> :        [#21]  Integer  and  floating  types are collectively called
> :        arithmetic types. Arithmetic types  and  pointer  types  are
> :        collectively  called scalar types. Array and structure types
> :        are collectively called aggregate types.34)

> : [snip]

> OK, so I chose the name poorly. But why doesn't C have *the aggregate
> types that I was meaning*?

Joona, C gives you the ability to define such types, with vast ease and
flexibility, using structs for example. Aren't you forgetting about the
whole beauty of C, it's immense flexibility ?

If you want a pair, why wouldn't you want a triple ? Or a combination
of 2 floats and 53 chars etc... ? C lets you do all that using structures.

If you're starting to talk about other languages, just use C++. The STL
defines a very useful generic 'pair' class.



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:
> Joona, C gives you the ability to define such types, with vast ease and
> flexibility, using structs for example. Aren't you forgetting about the
> whole beauty of C, it's immense flexibility ?

> If you want a pair, why wouldn't you want a triple ? Or a combination
> of 2 floats and 53 chars etc... ? C lets you do all that using structures.

> If you're starting to talk about other languages, just use C++. The STL
> defines a very useful generic 'pair' class.

Perl makes your life even easier with this. It's arrays can hold everything,
ints and strings together, any number (in reasonable limits) of them
etc.


Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:
> (int, int) order(int x, int y)

If you want MATLAB, you know where to get it.
--
"In My Egotistical Opinion, most people's C programs should be indented six
 feet downward and covered with dirt." -- Blair P. Houghton


Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:
> Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
> algorithms, it would sometimes be useful to return an ordered pair (or
> n-tuple) of types from a function. Such as this trivial example:
> (int, int) order(int x, int y)
> {
>   if (x<=y)
>     return (x, y);
>   else
>     return (y, x);
> }

actually, this is a valid question.  i see no reason why i should be able to
do this:
        function(3, 4);
but not this:
        (x, y) = function();

the suggestions so far are that you can return multiple values because you
can wrap them inside an aggregate type.  so why don't you have to wrap
multiple arguments in an aggregate type?  personally i find it quite
inconsistent.

--
 /"\                                                 m i k e   b u r r e l l

  X        AGAINST HTML MAIL,



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types


Quote:
> Why doesn't C use aggregate types? (Or type aggregates.) In
mathematical
> algorithms, it would sometimes be useful to return an ordered pair (or
> n-tuple) of types from a function. Such as this trivial example:

> (int, int) order(int x, int y)
> {
>   if (x<=y)
>     return (x, y);
>   else
>     return (y, x);
> }

> Then it could be used such as:

> int main(void)
> {
>   int a=100;
>   int b=50;
>   (a, b)=order(a, b);
>   return 0;
> }

> As the scalar type names are always given in the aggregate types, I
> don't think this introduces any ambiguity. Is it just because of
> difficulty of implementation this kind of thing isn't possible in C?

> <OT>
> Are there any languages (preferably similar to C) where this is
> possible?
> </OT>

I came across a language that allowed this sort of thing. Turing maybe?

--
"Beware of bugs in the above code; I have only proved it correct,
not tried it."
                                            Donald Knuth

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types


...
 > >   (a, b)=order(a, b);
...
 > > Are there any languages (preferably similar to C) where this is
 > > possible?
 >
 > I came across a language that allowed this sort of thing. Turing maybe?

I know that it can in Mesa: [a, b] := order [a, b]; where the arguments
are input only.  You can also do it in Python, from a program I have:
   ready, d1, d2 = select.select([self.sock], [], [], timeout)
.
--
dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/



Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:
>I know that it can [be done] in Mesa: [a, b] := order [a, b];

(Or, similarly, to swap variables x and y: "[x, y] := [y, x];".  The
:= is really a left-pointing arrow.)

One thing that may surprise people -- or maybe not -- is that in Mesa,
the square brackets are an "aggregation" operator.  In other words, when
you write one of the above, you are writing the equivalent of the C
code:

        struct pair tmp;
        tmp.a = a;
        tmp.b = b;
        tmp = order(tmp);
        a = tmp.a;
        b = tmp.b;

So you can just write this in C, and hope that C compilers optimize
it properly -- just as you have to hope that Mesa compilers optimize
it properly. :-)

In other words, the Mesa syntax is nice in that it is compact, but it
offers no real abilities that are not already in C.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sun, 30 Mar 2003 03:00:00 GMT  
 Aggregate types


Quote:
> Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
> algorithms, it would sometimes be useful to return an ordered pair (or
> n-tuple) of types from a function. Such as this trivial example:

History, specifically including that `,` already has a meaning.

(Note to other readers: Joona's aggregate types are *not* well-modelled
by C structures. For example, if the expression `E` delivers two results,
one would like to be able to write

    (alpha XX beta) = E;

to assign the first value to `alpha` and the second to `beta`, without
having to fabricate a structure type and declaration to do so. `XX` would
naturally be spelt `,`, but that has a meaning inconsistent with Joona's
aggregation.)

Quote:
> <OT>
> Are there any languages (preferably similar to C) where this is
> possible?
> </OT>

Pop11. But it's not remotely like C, and it's dynamically typed, with
a postscript-style open stack.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Mon, 31 Mar 2003 03:00:00 GMT  
 Aggregate types

Quote:

> (Note to other readers: Joona's aggregate types are *not* well-modelled
> by C structures. For example, if the expression `E` delivers two results,
> one would like to be able to write

>     (alpha XX beta) = E;

> to assign the first value to `alpha` and the second to `beta`, without
> having to fabricate a structure type and declaration to do so. `XX` would
> naturally be spelt `,`, but that has a meaning inconsistent with Joona's
> aggregation.)

From that point of view, it is too bad that, in C99, one cannot
write
        (struct foo){alpha, beta} = E;
but AFAICT C99's compound literals don't work that way.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz


Mon, 31 Mar 2003 03:00:00 GMT  
 Aggregate types
mike burrell schrieb:

Quote:


> > Why doesn't C use aggregate types? (Or type aggregates.) In mathematical
> > algorithms, it would sometimes be useful to return an ordered pair (or
> > n-tuple) of types from a function. Such as this trivial example:

> > (int, int) order(int x, int y)
> > {
> >   if (x<=y)
> >     return (x, y);
> >   else
> >     return (y, x);
> > }

> actually, this is a valid question.  i see no reason why i should be able to
> do this:
>         function(3, 4);
> but not this:
>         (x, y) = function();

> the suggestions so far are that you can return multiple values because you
> can wrap them inside an aggregate type.  so why don't you have to wrap
> multiple arguments in an aggregate type?  personally i find it quite
> inconsistent.

Well, you don't need to, but you can, and sometimes it is useful I
think. I sometimes used it to avoid using globals without having to
pass ten parameters to a lot of different functions. Putting them
into a struct and passing a pointer to that struct made life much
easier.
Ok, I agree, these variables _are_ then "kind of global", but they
are still invisible in functions which don't use them at all.

Kind regards
Robert

- Show quoted text -

Quote:

> --
>  /"\                                                 m i k e   b u r r e l l

>   X        AGAINST HTML MAIL,




Wed, 02 Apr 2003 14:24:30 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Array-Like Aggregate Type with negative Indices???

2. Type Casting between Base and Derived Type (Serialization of objects with Type members)

3. Variables and types of type "Type"

4. Aggregate Initialization of Managed Structure

5. Internal Compiler Error with aggregate initialization

6. Aggregates initialization and C2552

7. Need code for the initialization of C99 aggregates

8. Automatic aggregate initialisation (revisited)

9. non-constants in aggregate initialization

10. Automatic aggregate initialisation

11. Aggregate initialistation

12. Why not return an aggregate?

 

 
Powered by phpBB® Forum Software