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 fn compose = [Math.sin, Math.cos, function (x) { return Math.pow(xf, 3g); { }];
var inv = [Math.asin, Math.acos, function (x) { return Math.pow(x, 1/3); }];
 
var compose = function (f, g) {
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>mysub $cubecompose =(&g, *&f) { return { g f $^x **} 3;}
 
my $cube = * ** 3;
my &croot = * ** (1/3); # Equivalent to 'sub croot ($x) { $x**(1/3) }'
sub compose (&g, &f) { return { g f $^x } }
my @functions = &sin, &cos, $cube;
my @inverses = &asin, &acos, &croot;
Line 910:
 
=={{header|Scheme}}==
<lang scheme>(define (cubecompose xf g) (exptlambda (x) 3(f (g x))))
(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 (compose f g) (lambda (x) (f (g x))))
 
(define x 0.5)
Anonymous user