langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,36 @@
import COM.ibm.netrexx.process.
class JensensDevice
properties static
interpreter=NetRexxA
exp=Rexx ""
termMethod=Method
method main(x=String[]) static
say sum('i',1,100,'1/i')
method sum(i,lo,hi,term) static SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
sum=0
loop iv=lo to hi
sum=sum+termeval(i,iv,term)
end
return sum
method termeval(i,iv,e) static returns Rexx SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
if e\=exp then interpreter=null
exp=e
if interpreter=null then do
termpgm='method term('i'=Rexx) static returns rexx;return' e
fw=FileWriter("termpgm.nrx")
fw.write(termpgm,0,termpgm.length)
fw.close
interpreter=NetRexxA()
interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
termClass=interpreter.getClassObject(null,'termpgm')
classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
termMethod=termClass.getMethod('term', classes)
end
return Rexx termMethod.invoke(null,[iv])

View file

@ -0,0 +1,13 @@
let i = ref 42 (* initial value doesn't matter *)
let sum' i lo hi term =
let result = ref 0. in
i := lo;
while !i <= hi do
result := !result +. term ();
incr i
done;
!result
let () =
Printf.printf "%f\n" (sum' i 1 100 (fun () -> 1. /. float !i))

View file

@ -0,0 +1,23 @@
bundle Default {
class Jensens {
i : static : Int;
function : Sum(lo : Int, hi : Int, term : () ~ Float) ~ Float {
temp := 0.0;
for(i := lo; i <= hi; i += 1;) {
temp += term();
};
return temp;
}
function : term() ~ Float {
return 1.0 / i;
}
function : Main(args : String[]) ~ Nil {
Sum(1, 100, term() ~ Float)->PrintLine();
}
}
}

View file

@ -0,0 +1,14 @@
declare
fun {Sum I Lo Hi Term}
Temp = {NewCell 0.0}
in
I := Lo
for while:@I =< Hi do
Temp := @Temp + {Term}
I := @I + 1
end
@Temp
end
I = {NewCell unit}
in
{Show {Sum I 1 100 fun {$} 1.0 / {Int.toFloat @I} end}}

View file

@ -0,0 +1,6 @@
declare
fun {Sum Lo Hi F}
{FoldL {Map {List.number Lo Hi 1} F} Number.'+' 0.0}
end
in
{Show {Sum 1 100 fun {$ I} 1.0/{Int.toFloat I} end}}

View file

@ -0,0 +1,10 @@
sub sum($i is rw, $lo, $hi, &term) {
my $temp = 0;
loop ($i = $lo; $i <= $hi; $i++) {
$temp += term;
}
return $temp;
}
my $i;
say sum $i, 1, 100, { 1 / $i };

View file

@ -0,0 +1,17 @@
Prototype.d func()
Global i
Procedure.d Sum(*i.Integer, lo, hi, *term.func)
Protected Temp.d
For i=lo To hi
temp + *term()
Next
ProcedureReturn Temp
EndProcedure
Procedure.d term_func()
ProcedureReturn 1/i
EndProcedure
Answer.d = Sum(@i, 1, 100, @term_func())

View file

@ -0,0 +1,7 @@
public num Jenssen(int lo, int hi, num (int i) term){
temp = 0;
while (lo <= hi){
temp += term(lo);
lo += 1;}
return temp;
}

View file

@ -0,0 +1,2 @@
rascal>Jenssen(1, 100, num(int i){return 1.0/i;})
num: 5.18737751763962026080511767565825315790897212670845165317653395662

View file

@ -0,0 +1,19 @@
$ include "seed7_05.s7i";
include "float.s7i";
var integer: i is 0;
const func float: sum (inout integer: i, in integer: lo, in integer: hi,
ref func float: term) is func
result
var float: sum is 0.0
begin
for i range lo to hi do
sum +:= term;
end for;
end func;
const proc: main is func
begin
writeln(sum(i, 1, 100, 1.0/flt(i)) digits 6);
end func;

View file

@ -0,0 +1,15 @@
val i = ref 42 (* initial value doesn't matter *)
fun sum' (i, lo, hi, term) = let
val result = ref 0.0
in
i := lo;
while !i <= hi do (
result := !result + term ();
i := !i + 1
);
!result
end
val () =
print (Real.toString (sum' (i, 1, 100, fn () => 1.0 / real (!i))) ^ "\n")