comp.lang.ada
 help / color / mirror / Atom feed
From: Gustaf Thorslund <gustaf@thorslund.org>
Subject: Re: X : Real_Vector := Solve(A => A, X => B); -- Why X as argument name?
Date: Mon, 24 Dec 2012 21:42:30 +0100
Date: 2012-12-24T21:42:30+01:00	[thread overview]
Message-ID: <87pq1zkwqx.fsf@katthult.thorslund.org> (raw)
In-Reply-To: ajqia9FfiahU1@mid.individual.net

Niklas Holsti <niklas.holsti@tidorum.invalid> writes:

> Or you could ask, why use "X" as the name of a local variable?

Since it's a common name of an unknown in the context of linear
equations.

>
>>    
>> begin
>>    for I in X'Range loop
>>       Put_Line(Float'Image(X(I)));
>>    end loop;
>> end Why_X;
>> 
>> -- Executing the program gives
>> -- $ ./why_x 
>> --  6.00000E+00
>> -- So we found the expected value of x.
>
> Do you see that as a problem? :-)

No :-) But someone reading
  http://www.adaic.org/resources/add_content/standards/05rat/html/Rat-7-6.html#I1361

without a knowing what linear equations are could get a bit confused.

(in the middle of the page there is an equation "Ax = y")

>> -- Was it a misstake to use X as argument name in Solve?
>
> Do you mean, is it an error that your program uses "X" as the name of a
> local variable, when it also calls a function that has a formal
> parameter called "X"?
>
> No, it is not an error and is quite legal. The compiler knows from the
> named-association syntax ("X => ...") that "X" refers to the name of a
> formal parameter.

No, I used X intentionally to show the why it can be confusing.

> Or do you mean to ask if it was a mistake in the definition of
> Ada.Numerics.Real_Arrays.Solve to give the second formal parameter the
> name "X", given that "X" is a common name in client code?

Right!

> That is a matter of coding style and habit. (I would probably have
> chosen the name "B" for the second parameter, since "X" suggests
> "unknowns" to me, while "A" and "B" suggest "knowns".) And most coding
> style guidelines recommend against single-letter names like "X" in
> application programs, so "X" should not really be a common name.

"X" could be a common name in short examples or even a local
variable. Just like "I" and "J" in loops.

> Or do you mean to ask if it is mistake that your program uses the name
> "X" for a local variable, which the reader can confuse with the formal
> parameter "X"?

No.

> If you have no important reason to call your local variable "X", it
> would be slightly clearer to choose a different name, at least for
> readers not yet familiar with Ada.

For readers not familiar with Ada but familiar with linear equations one
might as well write:

  X := Solve (A, B);

From what I see the argument name "X" doesn't really add any value
anyway.

/Gustaf



  parent reply	other threads:[~2012-12-24 20:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-23 22:28 X : Real_Vector := Solve(A => A, X => B); -- Why X as argument name? Gustaf Thorslund
2012-12-24  8:33 ` Niklas Holsti
2012-12-24 11:50   ` Simon Wright
2012-12-24 12:58     ` Niklas Holsti
2012-12-24 21:25     ` Gustaf Thorslund
2012-12-24 20:42   ` Gustaf Thorslund [this message]
2012-12-24 20:10 ` jpwoodruff
replies disabled

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