Topic variable: Difference between revisions

From Rosetta Code
Content added Content deleted
(reformulation)
Line 19: Line 19:
=={{header|Perl}}==
=={{header|Perl}}==


In Perl the topic variable is $_. It is the default argument for many functions, including the square root. 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 the square root. It is also the default parameter for loops:


<lang Perl>my $_ = 3;
<lang Perl>my $_ = 3;
print for $_**2, "\n", sqrt;</lang>
print for $_**2, "\n", sqrt;</lang>

The topic parameter is lexical, so its use can be nested into several lexical scopes. However, assignent to the topic variable at loop declaration is not lexical, and a my function is needed to enable the loops to nest:

<lang perl>for ($_ = 0; $_ <= 9; $_++) {
print "Outer";
print "$_\n";
# The inner loop will not nest properly unless
# it is preceded by a my statement
my $_; # This is required to nest the inner loop
for ($_ = 0; $_ <= 9; $_++) {
print "Inner";
print "$_\n";
}
}</lang>


=={{header|Perl 6}}==
=={{header|Perl 6}}==

Revision as of 23:30, 9 February 2013

Topic variable is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

Demonstrate the utilization and behaviour of the topic variable within the language and explain or demonstrate how the topic variable behaves under different levels of nesting or scope, if this applies, within the language.

For instance you can (but you don't have to) show how the topic variable can be used by affecting the number to it, and then compute its square and square root.

J

With this new definition of topic variables, the closest thing J has to a topic variable is probably dummy variables used in function definitions, because we always omit declaring which variables they are, and because we may omit them entirely. But, we still need to place the function in the context of the value it's being used on.

Thus, for example:

<lang J> example=: *:, %: NB. *: is square, %: is square root

  example 3

9 1.73205</lang>

Or course, if it's crucial to the concept of topic variables that they not be constrained to definitions of things like functions, then it might be argued that J does not have them.

Perl

In Perl the topic variable is $_. It is the default argument for many functions, including the square root. It is also the default parameter for loops:

<lang Perl>my $_ = 3; print for $_**2, "\n", sqrt;</lang>

The topic parameter is lexical, so its use can be nested into several lexical scopes. However, assignent to the topic variable at loop declaration is not lexical, and a my function is needed to enable the loops to nest:

<lang perl>for ($_ = 0; $_ <= 9; $_++) {

 print "Outer";
 print "$_\n";
 # The inner loop will not nest properly unless
 # it is preceded by a my statement
 my $_;    # This is required to nest the inner loop
 for ($_ = 0; $_ <= 9; $_++) {
   print "Inner";
   print "$_\n";
 }

}</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.

This example is incomplete. Actually need to do the details of the task, and not just talk about it. Please ensure that it meets all task requirements and remove this message.