User:Coderjoe/Sandbox2: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=={{header| |
=={{header|Oz}}== |
||
To be executed in the REPL. |
|||
{{trans|Python}} |
|||
<lang Nemerle>using System; |
|||
using System.Console; |
|||
using System.Math; |
|||
using Nemerle.Collections.NCollectionsExtensions; |
|||
<lang oz>declare |
|||
module FirstClassFunc |
|||
{ |
|||
Main() : void |
|||
{ |
|||
def cube = fun (x) {x * x * x}; |
|||
def croot = fun (x) {Pow(x, 1.0/3.0)}; |
|||
def compose = fun(f, g) {fun (x) {f(g(x))}}; |
|||
def funcs = [Sin, Cos, cube]; |
|||
def ifuncs = [Asin, Acos, croot]; |
|||
WriteLine($[compose(f, g)(0.5) | (f, g) in ZipLazy(funcs, ifuncs)]); |
|||
} |
|||
}</lang> |
|||
fun {Compose F G} |
|||
=={{header|newLISP}}== |
|||
fun {$ X} |
|||
<lang newLISP>> (define (compose f g) (expand (lambda (x) (f (g x))) 'f 'g)) |
|||
{F {G X}} |
|||
(lambda (f g) (expand (lambda (x) (f (g x))) 'f 'g)) |
|||
end |
|||
> (define (cube x) (pow x 3)) |
|||
end |
|||
(lambda (x) (pow x 3)) |
|||
> (define (cube-root x) (pow x (div 1 3))) |
|||
(lambda (x) (pow x (div 1 3))) |
|||
> (define functions '(sin cos cube)) |
|||
(sin cos cube) |
|||
> (define inverses '(asin acos cube-root)) |
|||
(asin acos cube-root) |
|||
> (map (fn (f g) ((compose f g) 0.5)) functions inverses) |
|||
(0.5 0.5 0.5) |
|||
</lang> |
|||
fun {Cube X} X*X*X end |
|||
=={{header|OCaml}}== |
|||
<lang ocaml># let cube x = x ** 3. ;; |
|||
fun {CubeRoot X} {Number.pow X 1.0/3.0} end |
|||
val cube : float -> float = <fun> |
|||
in |
|||
for |
|||
F in [Float.sin Float.cos Cube] |
|||
I in [Float.asin Float.acos CubeRoot] |
|||
do |
|||
{Show {{Compose I F} 0.5}} |
|||
end |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
|||
# let croot x = x ** (1. /. 3.) ;; |
|||
{{works with|PARI/GP|2.4.2 and above}} |
|||
val croot : float -> float = <fun> |
|||
<lang parigp>compose(f,g)={ |
|||
x -> f(g(x)) |
|||
}; |
|||
fcf()={ |
|||
# let compose f g = fun x -> f (g x) ;; (* we could have written "let compose f g x = f (g x)" but we show this for clarity *) |
|||
my(A,B); |
|||
val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun> |
|||
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> |
|||
=={{header|Perl}}== |
|||
# let funclist = [sin; cos; cube] ;; |
|||
<lang perl>use Math::Complex ':trig'; |
|||
val funclist : (float -> float) list = [<fun>; <fun>; <fun>] |
|||
sub compose { |
|||
# let funclisti = [asin; acos; croot] ;; |
|||
my ($f, $g) = @_; |
|||
val funclisti : (float -> float) list = [<fun>; <fun>; <fun>] |
|||
sub { |
|||
$f -> ($g -> (@_)); |
|||
}; |
|||
} |
|||
my $cube = sub { $_[0] ** (3) }; |
|||
# List.map2 (fun f inversef -> (compose inversef f) 0.5) funclist funclisti ;; |
|||
my $croot = sub { $_[0] ** (1/3) }; |
|||
- : float list = [0.5; 0.499999999999999889; 0.5]</lang> |
|||
my @flist1 = ( \&Math::Complex::sin, \&Math::Complex::cos, $cube ); |
|||
=={{header|Octave}}== |
|||
my @flist2 = ( \&asin, \&acos, $croot ); |
|||
<lang octave>function r = cube(x) |
|||
r = x.^3; |
|||
endfunction |
|||
print join "\n", map { |
|||
function r = croot(x) |
|||
compose($flist1[$_], $flist2[$_]) -> (0.5) |
|||
r = x.^(1/3); |
|||
} 0..2;</lang> |
|||
endfunction |
|||
=={{header|Perl 6}}== |
|||
compose = @(f,g) @(x) f(g(x)); |
|||
{{works with|Rakudo|2011.06}} |
|||
<lang perl6>sub compose (&g, &f) { return { g f $^x } } |
|||
f1 = {@sin, @cos, @cube}; |
|||
f2 = {@asin, @acos, @croot}; |
|||
my $x = *.sin; |
|||
my $xi = *.asin; |
|||
disp(compose(f1{i}, f2{i})(.5)) |
|||
my $y = *.cos; |
|||
endfor</lang> |
|||
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> |
Revision as of 20:37, 16 July 2011
Oz
To be executed in the REPL.
<lang oz>declare
fun {Compose F G} fun {$ X} {F {G X}} end end
fun {Cube X} X*X*X end
fun {CubeRoot X} {Number.pow X 1.0/3.0} end
in
for F in [Float.sin Float.cos Cube] I in [Float.asin Float.acos CubeRoot] do {Show {{Compose I F} 0.5}} end
</lang>
PARI/GP
<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>
Perl
<lang perl>use Math::Complex ':trig';
sub compose {
my ($f, $g) = @_; sub { $f -> ($g -> (@_)); };
}
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 {
compose($flist1[$_], $flist2[$_]) -> (0.5)
} 0..2;</lang>
Perl 6
<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:
0.5 0.5 0.5