Anonymous user
First-class functions: Difference between revisions
m
no edit summary
No edit summary |
mNo edit summary |
||
Line 621:
assuming the print function is provided by the environment, like a stand-alone shell. In browsers, use alert(), document.write() or similar
<lang javascript>var
var inv = [Math.asin, Math.acos, function (x) { return Math.pow(x, 1/3); }];▼
return function (x) {
return f(g(x));
};
};
var fn = [Math.sin, Math.cos, function (x) { return Math.pow(x, 3); }];
▲var inv = [Math.asin, Math.acos, function (x) { return Math.pow(x, 1/3); }];
(function () {
Line 748:
=={{header|Perl}}==
<lang perl>use Math::Complex ':trig';
my $cube = sub { $_[0] ** (3) };▼
my $croot = sub { $_[0] ** (1/3) };▼
my @flist1 = ( \&Math::Complex::sin, \&Math::Complex::cos, $cube );▼
my @flist2 = ( \&asin, \&acos, $croot );▼
sub compose {
Line 762 ⟶ 756:
};
}
▲my $cube = sub { $_[0] ** (3) };
▲my $croot = sub { $_[0] ** (1/3) };
▲my @flist1 = ( \&Math::Complex::sin, \&Math::Complex::cos, $cube );
▲my @flist2 = ( \&asin, \&acos, $croot );
print join "\n", map {
Line 770:
{{works with|Rakudo|2010.07}}
<lang perl6>
my $cube = * ** 3;
my &croot = * ** (1/3); # Equivalent to 'sub croot ($x) { $x**(1/3) }'
my @functions = &sin, &cos, $cube;
my @inverses = &asin, &acos, &croot;
Line 910:
=={{header|Scheme}}==
<lang scheme>(define (
(define (cube x) (expt x 3))
(define (cube-root x) (expt x (/ 1 3)))
(define function (list sin cos cube))
(define inverse (list asin acos cube-root))
(define x 0.5)
|