User:Coderjoe/Sandbox2: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<lang parigp>compose(f,g)={
=={{header|Perl}}==
x -> f(g(x))
<lang perl>use Math::Complex ':trig';
};


fcf()={
sub compose {
my ($f, $g) = @_;
my(A,B);
A=[x->sin(x), x->cos(x), x->x^2];
B=[x->asin(x), x->acos(x), x->sqrt(x)];
sub {
for(i=1,#A,
$f -> ($g -> (@_));
print(compose(A[i],B[i])(.5))
};
)
}
};</lang>

Usage note: In Pari/GP 2.4.3 the vectors can be written as
my $cube = sub { $_[0] ** (3) };
<lang parigp> A=[sin, cos, x->x^2];
my $croot = sub { $_[0] ** (1/3) };
B=[asin, acos, x->sqrt(x)];</lang>

my @flist1 = ( \&Math::Complex::sin, \&Math::Complex::cos, $cube );
my @flist2 = ( \&asin, \&acos, $croot );

print join "\n", map {
compose($flist1[$_], $flist2[$_]) -> (0.5)
} 0..2;</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2011.06}}

<lang perl6>sub compose (&g, &f) { return { g f $^x } }

my $x = *.sin;
my $xi = *.asin;
my $y = *.cos;
my $yi = *.acos;
my $z = * ** 3;
my $zi = * ** (1/3);
my @functions = $x, $y, $z;
my @inverses = $xi, $yi, $zi;
for @functions Z @inverses { say compose($^g, $^f)(.5) }</lang>
Output:
<pre>0.5
0.5
0.5</pre>

Latest revision as of 20:38, 16 July 2011

<lang parigp>compose(f,g)={

 x -> f(g(x))

};

fcf()={

 my(A,B);
 A=[x->sin(x), x->cos(x), x->x^2];
 B=[x->asin(x), x->acos(x), x->sqrt(x)];
 for(i=1,#A,
   print(compose(A[i],B[i])(.5))
 )

};</lang> Usage note: In Pari/GP 2.4.3 the vectors can be written as <lang parigp> A=[sin, cos, x->x^2];

 B=[asin, acos, x->sqrt(x)];</lang>