Topic variable: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Perl}}: adding linefeed)
Line 17: Line 17:
=={{header|Perl}}==
=={{header|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.
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.


<lang Perl>my $_ = 3;
<lang Perl>my $_ = 3;

Revision as of 23:09, 27 January 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.

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

This example is incorrect. It does not accomplish the given task. Please fix the code and remove this message.

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

<lang Perl>my $_ = 3; print for $_**2, "\n", 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.

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.