* Re: Ada Queue
2000-04-06 0:00 ` Joseph T
@ 2000-04-06 0:00 ` Robert Dewar
2000-04-06 0:00 ` Ted Dennison
2000-04-06 0:00 ` Joseph T
2000-04-06 0:00 ` Ted Dennison
2000-04-06 0:00 ` tmoran
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 ` Ted Dennison
2000-04-06 0:00 ` Joseph T
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 ` Robert Dewar
2000-04-06 0:00 ` Ted Dennison
@ 2000-04-06 0:00 ` Joseph T
2000-04-07 0:00 ` Ole-Hjalmar Kristensen
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-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-06 0:00 ` Joseph T
2000-04-06 0:00 ` Robert Dewar
@ 2000-04-06 0:00 ` Ted Dennison
2000-04-06 0:00 ` Joseph T.
2000-04-06 0:00 ` tmoran
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-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
* 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-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 ` 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 ` 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 ` Joseph T
2000-04-06 0:00 ` Robert Dewar
2000-04-06 0:00 ` Ted Dennison
@ 2000-04-06 0:00 ` tmoran
2000-04-08 0:00 ` tmoran
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 ` 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