Topic variable

Revision as of 20:12, 11 March 2013 by rosettacode>Markhobley (categories)

Several programming languages offer syntax shortcuts to deal with the notion of "current" or "topic" variable. A topic variable is a special variable with a very short name which can also often be omitted.

Task
Topic variable
You are encouraged to solve this task according to the task description, using any language you may know.

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 assigning the number to it and then computing 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 or local keyword 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";
 }
 # Alternatively we can use a local keyword in the
 # inner loop declaration instead of a my statement
 for (local $_ = 0; $_ <= 9; $_++) {
   print "Alternative";
   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> The scope of the $_ variable is always lexical in Perl 6, though a function can access its caller's topic if it asks for it specially via CALLER::<$_>.

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.