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 @@
Twice[T] (f : T -> T, x : T) : T { f(f(x)) }

View file

@ -0,0 +1,6 @@
> (define (my-multiply a b) (* a b))
(lambda (a b) (* a b))
> (define (call-it f x y) (f x y))
(lambda (f x y) (f x y))
> (call-it my-multiply 2 3)
6

View file

@ -0,0 +1,8 @@
# let func1 f = f "a string";;
val func1 : (string -> 'a) -> 'a = <fun>
# let func2 s = "func2 called with " ^ s;;
val func2 : string -> string = <fun>
# print_endline (func1 func2);;
func2 called with a string
- : unit = ()

View file

@ -0,0 +1,6 @@
# let func f = f 1 2;;
val func : (int -> int -> 'a) -> 'a = <fun>
# Printf.printf "%d\n" (func (fun x y -> x + y));;
3
- : unit = ()

View file

@ -0,0 +1,16 @@
bundle Default {
class HighOrder {
function : Main(args : String[]) ~ Nil {
f := GetSize(String) ~ Int;
Print(f);
}
function : GetSize(s : String) ~ Int {
return s->Size();
}
function : Print(func : (String)~Int) ~ Nil {
func("Hello World!")->PrintLine();
}
}
}

View file

@ -0,0 +1,6 @@
function r = computeit(f, g, v)
r = f(g(v));
endfunction
computeit(@exp, @sin, pi/3)
computeit(@log, @cos, pi/6)

View file

@ -0,0 +1,5 @@
function r = computeit2(f, g, v)
r = f(feval(g, v));
endfunction
computeit2(@exp, "sin", pi/3)

View file

@ -0,0 +1,27 @@
'FUNCTION TO BE PASSED
'=====================
function f(double d,e) as double
return (d+e)*2
end function
'FUNCTION TAKING A FUNCTION AS AN ARGUMENT
'=========================================
function g(sys p) as string
declare function x(double d,e) as double at p
return x(10,11)
end function
'TEST: PASSING ADDRESS OF FUNCTION f
'===================================
'the name 'f' is combined with the prototype signature '#double#double'
'@' signifies the address of the function is being passed
print g(@f#double#double) 'result '42'

View file

@ -0,0 +1,6 @@
declare
fun {Twice Function X}
{Function {Function X}}
end
in
{Show {Twice Sqrt 81.0}} %% prints 3.0

View file

@ -0,0 +1,19 @@
secant_root(ff,a,b)={
e = eps() * 2;
aval=ff(a);
bval=ff(b);
while (abs(bval) > e,
oldb = b;
b = b - (b - a)/(bval - aval) * bval;
aval = bval;
bval = ff(b);
a = oldb
);
b
};
addhelp(secant_root, "secant_root(ff,a,b): Finds a root of ff between a and b using the secant method.");
eps()={
precision(2. >> (32 * ceil(default(realprecision) * 38539962 / 371253907)), 9)
};
addhelp(eps,"Returns machine epsilon for the current precision.");

View file

@ -0,0 +1,8 @@
f: procedure (g) returns (float);
declare g entry (float);
get (x);
put (g(x));
end f;
x = f(p); /* where "p" is the name of a function. */

View file

@ -0,0 +1,15 @@
program example(output);
function first(function f(x: real): real): real;
begin
first := f(1.0) + 2.0;
end;
function second(x: real): real;
begin
second := x/2.0;
end;
begin
writeln(first(second));
end.

View file

@ -0,0 +1,20 @@
program example;
type
FnType = function(x: real): real;
function first(f: FnType): real;
begin
first := f(1.0) + 2.0;
end;
{$F+}
function second(x: real): real;
begin
second := x/2.0;
end;
{$F-}
begin
writeln(first(second));
end.

View file

@ -0,0 +1,17 @@
sub twice(&todo) {
todo(); todo(); # declaring &todo also defines bare function
}
twice { say "Boing!" }
# output:
# Boing!
# Boing!
sub twice-with-param(&todo) {
todo(0); todo(1);
}
twice-with-param -> $time {
say "{$time+1}: Hello!"
}
# output:
# 1: Hello!
# 2: Hello!

View file

@ -0,0 +1,7 @@
;;; Define a function
define x_times_three_minus_1(x);
return(3*x-1);
enddefine;
;;; Pass it as argument to built-in function map and print the result
mapdata({0 1 2 3 4}, x_times_three_minus_1) =>

View file

@ -0,0 +1,2 @@
/x_times_3_sub_1 {3 * 1 sub}.
[0 1 2 3 4] {x_times_3_sub_1} map

View file

@ -0,0 +1,12 @@
Prototype.d func(*text$)
Procedure NumberTwo(arg$)
Debug arg$
EndProcedure
Procedure NumberOne(*p, text$)
Define MyFunc.func=*p
MyFunc(@text$)
EndProcedure
NumberOne(@NumberTwo(),"Hello Worldy!")

View file

@ -0,0 +1,32 @@
REBOL [
Title: "Function Argument"
Author: oofoe
Date: 2009-12-19
URL: http://rosettacode.org/wiki/Function_as_an_Argument
]
map: func [
"Apply function to contents of list, return new list."
f [function!] "Function to apply to list."
data [block! list!] "List to transform."
/local result i
][
result: copy [] repeat i data [append result f i] result]
square: func [
"Calculate x^2."
x [number!]
][x * x]
cube: func [
"Calculate x^3."
x [number!]
][x * x * x]
; Testing:
x: [1 2 3 4 5]
print ["Data: " mold x]
print ["Squared:" mold map :square x]
print ["Cubed: " mold map :cube x]
print ["Unnamed:" mold map func [i][i * 2 + 1] x]

View file

@ -0,0 +1,4 @@
: disp ( nq- )
do putn ;
31 [ ( n-n ) 100 * ] disp

View file

@ -0,0 +1,2 @@
define: #function -> [| :x | x * 3 - 1].
#(1 1 2 3 5 8) collect: function.

View file

@ -0,0 +1,8 @@
- fun func1 f = f "a string";
val func1 = fn : (string -> 'a) -> 'a
- fun func2 s = "func2 called with " ^ s;
val func2 = fn : string -> string
- print (func1 func2 ^ "\n");
func2 called with a string
val it = () : unit

View file

@ -0,0 +1,6 @@
- fun func f = f (1, 2);
val func = fn : (int * int -> 'a) -> 'a
- print (Int.toString (func (fn (x, y) => x + y)) ^ "\n");
3
val it = () : unit

View file

@ -0,0 +1,5 @@
Local map
Define map(f,l)=Func
Return seq(#f(l[i]),i,1,dim(l))
EndFunc
Disp map("sin", {0, π/6, π/4, π/3, π/2})

View file

@ -0,0 +1,8 @@
@(bind a @(let ((counter 0))
(mapcar (lambda (x y) (list (inc counter) x y))
'(a b c) '(t r s))))
@(output)
@ (repeat)
@ (rep)@a:@(last)@a@(end)
@ (end)
@(end)

View file

@ -0,0 +1,3 @@
[ ." First\n" ] is first
[ invoke ] is second
` first second

View file

@ -0,0 +1,3 @@
: twice 2 times ;
: hello "Hello, world!" print ;
[hello] twice

View file

@ -0,0 +1 @@
(autocomposition "f") "x" = "f" "f" "x"

View file

@ -0,0 +1,4 @@
#import flo
#cast %e
example = autocomposition(sqrt) 16.0

View file

@ -0,0 +1 @@
[first *].

View file

@ -0,0 +1 @@
[second i].

View file

@ -0,0 +1 @@
2 3 [first] second