comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada Queue
  2000-04-06  0:00     ` Robert Dewar
@ 2000-04-06  0:00       ` Joseph T
  2000-04-07  0:00         ` Ole-Hjalmar Kristensen
  2000-04-06  0:00       ` Ted Dennison
  1 sibling, 1 reply; 20+ messages in thread
From: Joseph T @ 2000-04-06  0:00 UTC (permalink / raw)



Perhaps I should have been clearer.  I created the ADA code based on the C
function found at the link I mentioned.  I would like to know why the author
would have chosen the methodology used.  I'm hoping someone can explain why
it was done the way it was done.

Robert Dewar <robert_dewar@my-deja.com> wrote:
>In article <38ecc752@news.hamilton.edu>,
>  "Joseph T" <thisthat7@hotmail.com> wrote:
>>
>> I need to explain why I chose to iterate through using the
>variable that was
>> passed instead of the temporary variable.  The only thing I
>can think of
>> is that I tried it and it worked.  Trial and error.  Perhaps
>it's not as
>> efficient, or logical, or conventional..but it really doesn't
>make a difference
>> as long as the proper variable of the queue is returned.
>>
>> I was wondering if any one could shed some light as to another
>reason this
>> solution works well, or why someone might choose it.
>>
>> Thank you so much for helping.
>
>
>This is completely bizarre, I can imagine someone being given
>this as an excercise, but if you wrote it, surely you can
>answer your own question here???
>
>Please be clearer about what you are asking.
>
>It sounds like
>
>I have written this code
>can anyone tell me why I wrote it this way
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00   ` Joseph T
@ 2000-04-06  0:00     ` tmoran
  2000-04-08  0:00       ` tmoran
  2000-04-06  0:00     ` Robert Dewar
  2000-04-06  0:00     ` Ted Dennison
  2 siblings, 1 reply; 20+ messages in thread
From: tmoran @ 2000-04-06  0:00 UTC (permalink / raw)


>I need to explain why I chose to iterate through using the variable that was
>passed instead of the temporary variable.  The only thing I can think of
>is that I tried it and it worked.  Trial and error.  Perhaps it's not as
  As in explain to your professor/boss?  "I tried it and it worked" may
be accurate, but it doesn't show you in the best light as a programmer. ;)

>I was wondering if any one could shed some light as to another reason this
>solution works well, or why someone might choose it.
  If you changed the variable name "New_Queue" to "Queue_Head", it
would be a little clearer, since New_Queue doesn't point to anything
new, and in particular not to any new queue, but in fact just
temporarily saves the old value of Q while you go off using the
variable Q, not as a pointer to a (complete) queue, but as a
pointer *into* the queue.  OTOH, if you changed
   New_Queue : Queue;
to
   Cursor : Queue := Q;
and dropped the
       New_Queue := Q;
and the
       Q := New_Queue;
and replaced all occurences of Q in the "true" branch of the "if"
with Cursor then the routine might be a little clearer, though
the fundamental algorithm is the same.




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00     ` Ted Dennison
@ 2000-04-06  0:00       ` Joseph T.
  2000-04-07  0:00         ` Ted Dennison
  2000-04-07  0:00         ` Ole-Hjalmar Kristensen
  0 siblings, 2 replies; 20+ messages in thread
From: Joseph T. @ 2000-04-06  0:00 UTC (permalink / raw)



Yes...I am looking for that techincal discussion...you're so slick.  =)

Let me ask you this...how come then, did the author of this page ( a CS professor
) choose the same method shown here:

http://www.cm.cf.ac.uk/Dave/C/node11.html

I think that the Ada replicates the method in C used by this professor. 
Does his code suffer from the same things you mentioned?

Thanks so much.





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Ada Queue
@ 2000-04-06  0:00 Joseph T.
  2000-04-06  0:00 ` Ted Dennison
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Joseph T. @ 2000-04-06  0:00 UTC (permalink / raw)



Can anyone corroborate why I chose to make this enqueue function using the
passed pointer to Q instead of the temp pointer to loop through the queue?
 Any suggestions, ideas, compliments, critiques are greatly appreciated.
 Please help.

procedure Enqueue(Q: in out Queue; E : Element_type) is
   New_Queue : Queue;

begin
   if Q /= null then
       New_Queue := Q;
       while Q.Rest /= null loop
          Q := Q.Rest;
       end loop;
       Q.Rest := new QueueNode;
       Q := Q.Rest;
       Q.Data := E;
       Q.Rest := null;
       Q := New_Queue;
   else
       Q := new QueueNode;
       Q.Data := E;
       Q.Rest := null;
   end if;
end Enqueue;




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00 Ada Queue Joseph T.
@ 2000-04-06  0:00 ` Ted Dennison
  2000-04-06  0:00   ` Joseph T
  2000-04-07  0:00 ` MaggieJohn
  2000-04-07  0:00 ` Simon Wright
  2 siblings, 1 reply; 20+ messages in thread
From: Ted Dennison @ 2000-04-06  0:00 UTC (permalink / raw)


In article <38eca724@news.hamilton.edu>,
  "Joseph T." <thisthat7@hotmail.com> wrote:
>
> Can anyone corroborate why I chose to make this enqueue function using
> the passed pointer to Q instead of the temp pointer to loop through
> the  queue? Any suggestions, ideas, compliments, critiques are greatly
> appreciated.

Perhaps, if you were to tell me why you chose to do that. I can hardly
corroborate something I don't know about.

Perhaps you should rephrase the question?

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00 ` Ted Dennison
@ 2000-04-06  0:00   ` Joseph T
  2000-04-06  0:00     ` tmoran
                       ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Joseph T @ 2000-04-06  0:00 UTC (permalink / raw)



I need to explain why I chose to iterate through using the variable that was
passed instead of the temporary variable.  The only thing I can think of
is that I tried it and it worked.  Trial and error.  Perhaps it's not as
efficient, or logical, or conventional..but it really doesn't make a difference
as long as the proper variable of the queue is returned.

I was wondering if any one could shed some light as to another reason this
solution works well, or why someone might choose it.

Thank you so much for helping.

Ted Dennison <dennison@telepath.com> wrote:
>In article <38eca724@news.hamilton.edu>,
>  "Joseph T." <thisthat7@hotmail.com> wrote:
>>
>> Can anyone corroborate why I chose to make this enqueue function using
>> the passed pointer to Q instead of the temp pointer to loop through
>> the  queue? Any suggestions, ideas, compliments, critiques are greatly
>> appreciated.
>
>Perhaps, if you were to tell me why you chose to do that. I can hardly
>corroborate something I don't know about.
>
>Perhaps you should rephrase the question?
>
>--
>T.E.D.
>
>http://www.telepath.com/~dennison/Ted/TED.html
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00   ` Joseph T
  2000-04-06  0:00     ` tmoran
@ 2000-04-06  0:00     ` Robert Dewar
  2000-04-06  0:00       ` Joseph T
  2000-04-06  0:00       ` Ted Dennison
  2000-04-06  0:00     ` Ted Dennison
  2 siblings, 2 replies; 20+ messages in thread
From: Robert Dewar @ 2000-04-06  0:00 UTC (permalink / raw)


In article <38ecc752@news.hamilton.edu>,
  "Joseph T" <thisthat7@hotmail.com> wrote:
>
> I need to explain why I chose to iterate through using the
variable that was
> passed instead of the temporary variable.  The only thing I
can think of
> is that I tried it and it worked.  Trial and error.  Perhaps
it's not as
> efficient, or logical, or conventional..but it really doesn't
make a difference
> as long as the proper variable of the queue is returned.
>
> I was wondering if any one could shed some light as to another
reason this
> solution works well, or why someone might choose it.
>
> Thank you so much for helping.


This is completely bizarre, I can imagine someone being given
this as an excercise, but if you wrote it, surely you can
answer your own question here???

Please be clearer about what you are asking.

It sounds like

I have written this code
can anyone tell me why I wrote it this way


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00     ` Robert Dewar
  2000-04-06  0:00       ` Joseph T
@ 2000-04-06  0:00       ` Ted Dennison
  1 sibling, 0 replies; 20+ messages in thread
From: Ted Dennison @ 2000-04-06  0:00 UTC (permalink / raw)


In article <8cikl8$anh$1@nnrp1.deja.com>,
  Robert Dewar <robert_dewar@my-deja.com> wrote:

> It sounds like
>
> I have written this code
> can anyone tell me why I wrote it this way

This reminds me a lot of the program which made the fortune of one of
the characters in the book Dirk Gentley's Holistic Detective Agency. It
was an AI program that, given a desired conclusion, could come up with a
series of plausable-sounding logical steps to show that it was a sound
one. Supposedly the DoD bought the whole project out and used it to help
acquire SDI funding from Congress. :-)


--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00   ` Joseph T
  2000-04-06  0:00     ` tmoran
  2000-04-06  0:00     ` Robert Dewar
@ 2000-04-06  0:00     ` Ted Dennison
  2000-04-06  0:00       ` Joseph T.
  2 siblings, 1 reply; 20+ messages in thread
From: Ted Dennison @ 2000-04-06  0:00 UTC (permalink / raw)


In article <38ecc752@news.hamilton.edu>,
  "Joseph T" <thisthat7@hotmail.com> wrote:
>
> I need to explain why I chose to iterate through using the variable
> that was passed instead of the temporary variable.  The only thing I
> can think of is that I tried it and it worked.  Trial and error.
> Perhaps it's not as efficient, or logical, or conventional..but it
> really doesn't make a difference as long as the proper variable of the
> queue is returned.
>
> I was wondering if any one could shed some light as to another reason
> this solution works well, or why someone might choose it.

Ahh. So, to extrapolate, you are probably a student having a
disagreement with an instructior or T.A. about the desirability of
writing your queue the way you did. So you want a technical discussion
of the merits of your code which will (you hope) be in your favor.

I guess you wouldn't have asked the question the way you did if you
could see any real benifits to the aproach you took over the other. In
this case I agree with you, I can't see any benifits to it either. I do
see two drawbacks to doing it the way you did it:

1. Its a bit more awkward, which makes it tougher to understand. In the
"make it run once" situation of a class assignment that may not seem
like much to you. But if you ever want to make a carrer of this (and
that is what your instructiors are tasked with training you for),
confusion often translates into bugs. Bugs translate into lost time and
effort. For a professional that is a *big* deal.

2. Your method requires an extra assignment to put the original value
back into the parameter. If you had used New_Queue for the iteration,
you wouldn't need that assignment. Admittedly one assignment doesn't
amount to much in a O(n) algorithm. But why waste CPU when there's no
compelling reason to do so?

Your instructor (or whoever) is quite right to challenge you on your
implementation of the algorithm, whichever way you implemented it. You
should be prepared to answer such challenges on any part of your code.
If your only answer is "that's the best way I could see to make it
work", that's fine. But in that situation, be open to better approaches.
No one wants to work with code that was developed by someone with a
"whatever works is good enough" attitude.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-07  0:00         ` Ole-Hjalmar Kristensen
@ 2000-04-07  0:00           ` Joseph T
  2000-04-07  0:00             ` Ted Dennison
  0 siblings, 1 reply; 20+ messages in thread
From: Joseph T @ 2000-04-07  0:00 UTC (permalink / raw)



>It would perhaps have been clearer for the reader to use a temporary
>variable to iterate, but there are no other drawbacks associated with
>his method, which is indeed a very common way of doing it in C.

Which method is a common way of doing it in C?  Using the temporary variable
to iterate or not?

( again, thanks so much )





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-07  0:00           ` Joseph T
@ 2000-04-07  0:00             ` Ted Dennison
  0 siblings, 0 replies; 20+ messages in thread
From: Ted Dennison @ 2000-04-07  0:00 UTC (permalink / raw)


In article <38edcbf2@news.hamilton.edu>,
  "Joseph T" <thisthat7@hotmail.com> wrote:
>
> >It would perhaps have been clearer for the reader to use a temporary
> >variable to iterate, but there are no other drawbacks associated with
> >his method, which is indeed a very common way of doing it in C.
>
> Which method is a common way of doing it in C?  Using the temporary
> variable to iterate or not?

In C all parameters are passed by copy-in. That means that *both*
the local variable and the parameter can be considered "temporaries".
Neither get passed back to the user, only the object of the "return"
goes back. So really either is just as good.

But for your Ada code you (quite reasonably) used an "in out" parameter.
But that means *any* assignment to that parameter potentially goes out
to the user. This is a very different situation.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00 Ada Queue Joseph T.
  2000-04-06  0:00 ` Ted Dennison
  2000-04-07  0:00 ` MaggieJohn
@ 2000-04-07  0:00 ` Simon Wright
  2000-04-09  0:00   ` Robert Dewar
  2 siblings, 1 reply; 20+ messages in thread
From: Simon Wright @ 2000-04-07  0:00 UTC (permalink / raw)


"Joseph T." <thisthat7@hotmail.com> writes:

> Can anyone corroborate why I chose to make this enqueue function using the
> passed pointer to Q instead of the temp pointer to loop through the queue?
>  Any suggestions, ideas, compliments, critiques are greatly appreciated.
>  Please help.
> 
> procedure Enqueue(Q: in out Queue; E : Element_type) is
>    New_Queue : Queue;
> 
> begin
>    if Q /= null then
>        New_Queue := Q;
>        while Q.Rest /= null loop
>           Q := Q.Rest;
>        end loop;
>        Q.Rest := new QueueNode;
>        Q := Q.Rest;
>        Q.Data := E;
>        Q.Rest := null;
>        Q := New_Queue;
>    else
>        Q := new QueueNode;
>        Q.Data := E;
>        Q.Rest := null;
>    end if;
> end Enqueue;

Personally I don't think you should mess with the parameter like
this. The temporary variable is the one you should use for traversing
the Queue to find its end.

   procedure Enqueue(Q: in out Queue; E : Element_type) is
   begin
     if Q /= null then
       declare
	 Last : Queue := Q;
       begin
	 while Last.Rest /= null loop
	   Last := Last.Rest;
	 end loop;
	 Last.Rest := new QueueNode'(Data => E,
				     Rest => null);
       end;
     else
       Q := new QueueNode'(Data => E,
			   Rest => null);
     end if;
   end Enqueue;





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00 Ada Queue Joseph T.
  2000-04-06  0:00 ` Ted Dennison
@ 2000-04-07  0:00 ` MaggieJohn
  2000-04-07  0:00   ` Ted Dennison
  2000-04-07  0:00 ` Simon Wright
  2 siblings, 1 reply; 20+ messages in thread
From: MaggieJohn @ 2000-04-07  0:00 UTC (permalink / raw)


Queue packages are usually services.  Youwant the calling procedure to define
and own the queue.   The calling process should define the element type and the
max length and stuff like that.

Also - add new nodes to the head of the queue.   You don't want to walk the
queue every time you add a node.

- Maggie

From: "Joseph T." thisthat7@hotmail.com wrote:

<snip>

procedure Enqueue(Q: in out Queue; E : Element_type) is
   New_Queue : Queue;

begin
   if Q /= null then
       New_Queue := Q;
       while Q.Rest /= null loop
          Q := Q.Rest;
       end loop;
       Q.Rest := new QueueNode;
       Q := Q.Rest;
       Q.Data := E;
       Q.Rest := null;
       Q := New_Queue;
   else
       Q := new QueueNode;
       Q.Data := E;
       Q.Rest := null;
   end if;
end Enqueue;





^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00       ` Joseph T.
@ 2000-04-07  0:00         ` Ted Dennison
  2000-04-07  0:00           ` Hyman Rosen
  2000-04-07  0:00         ` Ole-Hjalmar Kristensen
  1 sibling, 1 reply; 20+ messages in thread
From: Ted Dennison @ 2000-04-07  0:00 UTC (permalink / raw)


In article <38ed0123@news.hamilton.edu>,
  "Joseph T." <thisthat7@hotmail.com> wrote:
>
> Let me ask you this...how come then, did the author of this page ( a
> CS professor) choose the same method shown here:
>
> http://www.cm.cf.ac.uk/Dave/C/node11.html
>
> I think that the Ada replicates the method in C used by this
> professor. Does his code suffer from the same things you mentioned?

For those who don't want to bother surfing there, the relevent source
is:

listelement * AddItem (listelement * listpointer, int data) {

    listelement * lp = listpointer;

    if (listpointer != NULL) {
        while (listpointer -> link != NULL)
            listpointer = listpointer -> link;
        listpointer -> link = (struct listelement  *) malloc (sizeof
(listelement));
        listpointer = listpointer -> link;
        listpointer -> link = NULL;
        listpointer -> dataitem = data;
        return lp;
    }
    else {
        listpointer = (struct listelement  *) malloc (sizeof
(listelement));
        listpointer -> link = NULL;
        listpointer -> dataitem = data;
        return listpointer;
    }
}
(please forgive the funky formatting Deja may force on this)

To answer your question: No this is not the same. The equivalent of his
interface in (your) Ada would be:

function Enqueue (Q : in Queue; E : in Element_Type) return Queue is


He *does* still suffer from the extraneious assignment problem, but it
happens on the outside of the Enqueue call as an artifact of his
decision to use a function return for the new Queue value. Yours put it
on the inside, where its arguably just as wasteful, but a lot more
obvious.

The reasons I think he probably did it this way are:

1, C has no "in out" parameters like Ada. In order to emulate them, he'd
have to have used something like "listelement ** listpointer", a
construction that is both ugly and error-prone.

2. C automaticly gives you a function return, which subtly encourages
developers to find some use for it.

In other words, I think his interface has more to do with the failings
of C than anything else. This is a very good example of why its not
generally best to do wrote translations of C code into Ada.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00       ` Joseph T
@ 2000-04-07  0:00         ` Ole-Hjalmar Kristensen
  0 siblings, 0 replies; 20+ messages in thread
From: Ole-Hjalmar Kristensen @ 2000-04-07  0:00 UTC (permalink / raw)


"Joseph T" <thisthat7@hotmail.com> writes:

> Perhaps I should have been clearer.  I created the ADA code based on the C
> function found at the link I mentioned.  I would like to know why the author
> would have chosen the methodology used.  I'm hoping someone can explain why
> it was done the way it was done.
> 

If it was C, the answer is easy. All parameters are passed by value,
so this variable is just a local variable which has been assigned an
initial value. The original variable will never be changed.
So there's really no point in declaring an extra variable.


> Robert Dewar <robert_dewar@my-deja.com> wrote:
> >In article <38ecc752@news.hamilton.edu>,
> >  "Joseph T" <thisthat7@hotmail.com> wrote:
> >>
> >> I need to explain why I chose to iterate through using the
> >variable that was
> >> passed instead of the temporary variable.  The only thing I
> >can think of
> >> is that I tried it and it worked.  Trial and error.  Perhaps
> >it's not as
> >> efficient, or logical, or conventional..but it really doesn't
> >make a difference
> >> as long as the proper variable of the queue is returned.
> >>
> >> I was wondering if any one could shed some light as to another
> >reason this
> >> solution works well, or why someone might choose it.
> >>
> >> Thank you so much for helping.
> >
> >
> >This is completely bizarre, I can imagine someone being given
> >this as an excercise, but if you wrote it, surely you can
> >answer your own question here???
> >
> >Please be clearer about what you are asking.
> >
> >It sounds like
> >
> >I have written this code
> >can anyone tell me why I wrote it this way
> >
> >
> >Sent via Deja.com http://www.deja.com/
> >Before you buy.
> 

-- 
E pluribus Unix




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-07  0:00 ` MaggieJohn
@ 2000-04-07  0:00   ` Ted Dennison
  0 siblings, 0 replies; 20+ messages in thread
From: Ted Dennison @ 2000-04-07  0:00 UTC (permalink / raw)


In article <20000406215553.00917.00000166@ng-dh1.aol.com>,
  maggiejohn@aol.com (MaggieJohn) wrote:

> Also - add new nodes to the head of the queue.   You don't want to
> walk the queue every time you add a node.

Then you've to to walk the queue every time you *remove* a node. It's
really 6 of one, half a donzen of another.

Of course you can avoid the problem by making a "Queue" a record
containg pointers to the head and tail objects. But not with this
algorithm.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


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




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00       ` Joseph T.
  2000-04-07  0:00         ` Ted Dennison
@ 2000-04-07  0:00         ` Ole-Hjalmar Kristensen
  2000-04-07  0:00           ` Joseph T
  1 sibling, 1 reply; 20+ messages in thread
From: Ole-Hjalmar Kristensen @ 2000-04-07  0:00 UTC (permalink / raw)


"Joseph T." <thisthat7@hotmail.com> writes:

> Yes...I am looking for that techincal discussion...you're so slick.  =)
> 
> Let me ask you this...how come then, did the author of this page ( a CS professor
> ) choose the same method shown here:
> 
> http://www.cm.cf.ac.uk/Dave/C/node11.html
> 
> I think that the Ada replicates the method in C used by this professor. 
> Does his code suffer from the same things you mentioned?
> 

It would perhaps have been clearer for the reader to use a temporary
variable to iterate, but there are no other drawbacks associated with
his method, which is indeed a very common way of doing it in C.

But the algorithms can be made simpler (but perhaps not clearer) by
using a pointer to a pointer instead of a simple pointer.
For example, the AddItem code can be made much simpler by defining it
as listelement * AddItem (listelement ** listpointer, int data);
How to simplify the body of the function is left as an exercise to the
reader :-)

> Thanks so much.
> 

-- 
E pluribus Unix




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-07  0:00         ` Ted Dennison
@ 2000-04-07  0:00           ` Hyman Rosen
  0 siblings, 0 replies; 20+ messages in thread
From: Hyman Rosen @ 2000-04-07  0:00 UTC (permalink / raw)


Ted Dennison <dennison@telepath.com> writes:
> In article <38ed0123@news.hamilton.edu>,
> For those who don't want to bother surfing there, the relevent source is:
> 
> listelement * AddItem (listelement * listpointer, int data) {
>     listelement * lp = listpointer;
>     if (listpointer != NULL) {
>         while (listpointer -> link != NULL)
>             listpointer = listpointer -> link;
>         listpointer -> link = (struct listelement  *) malloc (sizeof
> (listelement));
>         listpointer = listpointer -> link;
>         listpointer -> link = NULL;
>         listpointer -> dataitem = data;
>         return lp;
>     }
>     else {
>         listpointer = (struct listelement  *) malloc (sizeof
> (listelement));
>         listpointer -> link = NULL;
>         listpointer -> dataitem = data;
>         return listpointer;
>     }
> }
> 
> The reasons I think he probably did it this way are:
> 1, C has no "in out" parameters like Ada. In order to emulate them, he'd
> have to have used something like "listelement ** listpointer", a
> construction that is both ugly and error-prone.
> 2. C automaticly gives you a function return, which subtly encourages
> developers to find some use for it.
> In other words, I think his interface has more to do with the failings
> of C than anything else. This is a very good example of why its not
> generally best to do wrote translations of C code into Ada.

If I were writing this (and I have, many times), I would certainly use
the "error-prone" number 1. Using the function return value is fine -
we should return the newly allocated node. My C code would be

listelement *AddItem(listelement **linkpointer, int data)
{
	listelement *node;
	while ((node = *linkpointer) != 0)
		linkpointer = &node->link;
	if ((node = *linkpointer = malloc(sizeof(listelement))) != 0)
	{
		node->link = 0;
		node->dataitem = data;
	}
	return node;
}




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-06  0:00     ` tmoran
@ 2000-04-08  0:00       ` tmoran
  0 siblings, 0 replies; 20+ messages in thread
From: tmoran @ 2000-04-08  0:00 UTC (permalink / raw)


Renaming and rearranging, you get the more Ada-ish, and IMHO clearer:

procedure Enqueue(Q: in out Queue; E : Element_type) is
   Cursor : Queue;
begin
   if Q = null then
       Q := new QueueNode'(Data=>E, Rest=>null);
   else
       Cursor := Q;
       while Cursor.Rest /= null loop
          Cursor := Cursor.Rest;
       end loop;
       Cursor.Rest := new QueueNode'(Data=>E, Rest=>null);
   end if;
end Enqueue;




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: Ada Queue
  2000-04-07  0:00 ` Simon Wright
@ 2000-04-09  0:00   ` Robert Dewar
  0 siblings, 0 replies; 20+ messages in thread
From: Robert Dewar @ 2000-04-09  0:00 UTC (permalink / raw)


In article <x7vu2hdoex3.fsf@pogner.demon.co.uk>,
  Simon Wright <simon@pogner.demon.co.uk> wrote:
> "Joseph T." <thisthat7@hotmail.com> writes:
>
> > Can anyone corroborate why I chose to make this enqueue
> > function using the passed pointer to Q instead of the temp
> > pointer to loop through the queue?

I must say I get a chuckle whenever I see this bizarre question
requoted. Maybe we can apply this attitude more widely, e.g.
perhaps we could here a politician finding himself doing
something peculiar asking in a speach:

"Can anyone corroborate why I chose to send troops to ...
instead of negotiating?"

:-)



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




^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2000-04-09  0:00 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-04-06  0:00 Ada Queue Joseph T.
2000-04-06  0:00 ` Ted Dennison
2000-04-06  0:00   ` Joseph T
2000-04-06  0:00     ` tmoran
2000-04-08  0:00       ` tmoran
2000-04-06  0:00     ` Robert Dewar
2000-04-06  0:00       ` Joseph T
2000-04-07  0:00         ` Ole-Hjalmar Kristensen
2000-04-06  0:00       ` Ted Dennison
2000-04-06  0:00     ` Ted Dennison
2000-04-06  0:00       ` Joseph T.
2000-04-07  0:00         ` Ted Dennison
2000-04-07  0:00           ` Hyman Rosen
2000-04-07  0:00         ` Ole-Hjalmar Kristensen
2000-04-07  0:00           ` Joseph T
2000-04-07  0:00             ` Ted Dennison
2000-04-07  0:00 ` MaggieJohn
2000-04-07  0:00   ` Ted Dennison
2000-04-07  0:00 ` Simon Wright
2000-04-09  0:00   ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox