Topic variable: Difference between revisions
(fix stupid sentence) |
(not using trigonometric functions anymore) |
||
Line 2: | Line 2: | ||
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted. |
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted. |
||
If your language has something similar, show how it can be used by affecting <math> |
If your language has something similar, show how it can be used by affecting <math>3</math> to the topic variable, and then compute its square and square root. |
||
=={{header|J}}== |
=={{header|J}}== |
||
{{incorrect}} <!-- Please upgrade your solution to the new task description --> |
|||
We might argue that all variables in J are topic variables. |
We might argue that all variables in J are topic variables. |
||
Line 19: | Line 19: | ||
In Perl the topic variable is $_. It is the default argument for many functions, including trigonometric ones. It is also the default parameter for loops. The topic parameter is lexical, so its use can be nested into several lexical scopes. |
In Perl the topic variable is $_. It is the default argument for many functions, including trigonometric ones. It is also the default parameter for loops. The topic parameter is lexical, so its use can be nested into several lexical scopes. |
||
<lang Perl>my $_ = |
<lang Perl>my $_ = 3; |
||
print for |
print for $_**2, sqrt;</lang> |
||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
Line 26: | Line 26: | ||
As in previous versions of Perl, in Perl6 the topic variable is $_. In addition to a direct affectation, it can also be set with the 'given' keyword. A method can be called from it with an implicit call: |
As in previous versions of Perl, in Perl6 the topic variable is $_. In addition to a direct affectation, it can also be set with the 'given' keyword. A method can be called from it with an implicit call: |
||
<lang Perl 6>given |
<lang Perl 6>given 3 { |
||
say . |
.say for $_**2, .sqrt; |
||
say .sin; |
|||
say .fmt: "%.4f"; |
|||
}</lang> |
}</lang> |
||
Revision as of 23:07, 27 January 2013
Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is basically nothing but some kind of a variable with a very short name which can also often be omitted.
If your language has something similar, show how it can be used by affecting to the topic variable, and then compute its square and square root.
J
We might argue that all variables in J are topic variables.
<lang J> 9!:11]4 NB. display 4 digits
pi=:1p1 NB. define the variable 2 1 o. pi NB. cosine (even function) and sine (odd function)
1.225e_16 _1</lang>
Note also that this particular implementation of J does not attempt to conceal the fact that floating point representation cannot precisely represent the value of pi.
Perl
In Perl the topic variable is $_. It is the default argument for many functions, including trigonometric ones. It is also the default parameter for loops. The topic parameter is lexical, so its use can be nested into several lexical scopes.
<lang Perl>my $_ = 3; print for $_**2, sqrt;</lang>
Perl 6
As in previous versions of Perl, in Perl6 the topic variable is $_. In addition to a direct affectation, it can also be set with the 'given' keyword. A method can be called from it with an implicit call:
<lang Perl 6>given 3 {
.say for $_**2, .sqrt;
}</lang>
Standard ML
If an SML expression is evaluated interactively (that is, the fragment of program provided interactively is an expression and not a variable initialization), the value of the expression is used to initialize a variable, it. This is a feature of the interactive shell only.