Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
22
Task/Y-combinator/Ceylon/y-combinator-1.ceylon
Normal file
22
Task/Y-combinator/Ceylon/y-combinator-1.ceylon
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Result(*Args) y1<Result,Args>(
|
||||
Result(*Args)(Result(*Args)) f)
|
||||
given Args satisfies Anything[] {
|
||||
|
||||
class RecursiveFunction(o) {
|
||||
shared Result(*Args)(RecursiveFunction) o;
|
||||
}
|
||||
|
||||
value r = RecursiveFunction((RecursiveFunction w)
|
||||
=> f(flatten((Args args) => w.o(w)(*args))));
|
||||
|
||||
return r.o(r);
|
||||
}
|
||||
|
||||
value factorialY1 = y1((Integer(Integer) fact)(Integer x)
|
||||
=> if (x > 1) then x * fact(x - 1) else 1);
|
||||
|
||||
value fibY1 = y1((Integer(Integer) fib)(Integer x)
|
||||
=> if (x > 2) then fib(x - 1) + fib(x - 2) else 2);
|
||||
|
||||
print(factorialY1(10)); // 3628800
|
||||
print(fibY1(10)); // 110
|
||||
11
Task/Y-combinator/Ceylon/y-combinator-2.ceylon
Normal file
11
Task/Y-combinator/Ceylon/y-combinator-2.ceylon
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Result(*Args) y2<Result,Args>(
|
||||
Result(*Args)(Result(*Args)) f)
|
||||
given Args satisfies Anything[] {
|
||||
|
||||
function r(Anything w) {
|
||||
assert (is Result(*Args)(Anything) w);
|
||||
return f(flatten((Args args) => w(w)(*args)));
|
||||
}
|
||||
|
||||
return r(r);
|
||||
}
|
||||
4
Task/Y-combinator/Ceylon/y-combinator-3.ceylon
Normal file
4
Task/Y-combinator/Ceylon/y-combinator-3.ceylon
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
Result(*Args) y3<Result, Args>(
|
||||
Result(*Args)(Result(*Args)) f)
|
||||
given Args satisfies Anything[]
|
||||
=> flatten((Args args) => f(y3(f))(*args));
|
||||
23
Task/Y-combinator/EchoLisp/y-combinator.echolisp
Normal file
23
Task/Y-combinator/EchoLisp/y-combinator.echolisp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
;; Ref : http://www.ece.uc.edu/~franco/C511/html/Scheme/ycomb.html
|
||||
|
||||
(define Y
|
||||
(lambda (X)
|
||||
((lambda (procedure)
|
||||
(X (lambda (arg) ((procedure procedure) arg))))
|
||||
(lambda (procedure)
|
||||
(X (lambda (arg) ((procedure procedure) arg)))))))
|
||||
|
||||
; Fib
|
||||
(define Fib* (lambda (func-arg)
|
||||
(lambda (n) (if (< n 2) n (+ (func-arg (- n 1)) (func-arg (- n 2)))))))
|
||||
(define fib (Y Fib*))
|
||||
(fib 6)
|
||||
→ 8
|
||||
|
||||
; Fact
|
||||
(define F*
|
||||
(lambda (func-arg) (lambda (n) (if (zero? n) 1 (* n (func-arg (- n 1)))))))
|
||||
(define fact (Y F*))
|
||||
|
||||
(fact 10)
|
||||
→ 3628800
|
||||
29
Task/Y-combinator/Eero/y-combinator.eero
Normal file
29
Task/Y-combinator/Eero/y-combinator.eero
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
typedef int (^Func)(int)
|
||||
typedef Func (^FuncFunc)(Func)
|
||||
typedef Func (^RecursiveFunc)(id) // hide recursive typing behind dynamic typing
|
||||
|
||||
Func fix(FuncFunc f)
|
||||
Func r(RecursiveFunc g)
|
||||
int s(int x)
|
||||
return g(g)(x)
|
||||
return f(s)
|
||||
return r(r)
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
autoreleasepool
|
||||
|
||||
Func almost_fac(Func f)
|
||||
return (int n | return n <= 1 ? 1 : n * f(n - 1))
|
||||
|
||||
Func almost_fib(Func f)
|
||||
return (int n | return n <= 2 ? 1 : f(n - 1) + f(n - 2))
|
||||
|
||||
fib := fix(almost_fib)
|
||||
fac := fix(almost_fac)
|
||||
|
||||
Log('fib(10) = %d', fib(10))
|
||||
Log('fac(10) = %d', fac(10))
|
||||
|
||||
return 0
|
||||
1
Task/Y-combinator/Oforth/y-combinator-1.oforth
Normal file
1
Task/Y-combinator/Oforth/y-combinator-1.oforth
Normal file
|
|
@ -0,0 +1 @@
|
|||
: Y(f) #[ f Y f perform ] ;
|
||||
2
Task/Y-combinator/Oforth/y-combinator-2.oforth
Normal file
2
Task/Y-combinator/Oforth/y-combinator-2.oforth
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: X(me, f) #[ me f me perform f perform ] ;
|
||||
: Y(f) #X f X ;
|
||||
11
Task/Y-combinator/Oforth/y-combinator-3.oforth
Normal file
11
Task/Y-combinator/Oforth/y-combinator-3.oforth
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
: almost-fact(n, f) n ifZero: [ 1 ] else: [ n n 1 - f perform * ] ;
|
||||
#almost-fact Y => fact
|
||||
|
||||
: almost-fib(n, f) n 1 <= ifTrue: [ n ] else: [ n 1 - f perform n 2 - f perform + ] ;
|
||||
#almost-fib Y => fib
|
||||
|
||||
: almost-Ackermann(m, n, f)
|
||||
m 0 == ifTrue: [ n 1 + return ]
|
||||
n 0 == ifTrue: [ 1 m 1 - f perform return ]
|
||||
n 1 - m f perform m 1 - f perform ;
|
||||
#almost-Ackermann Y => Ackermann
|
||||
7
Task/Y-combinator/Sidef/y-combinator.sidef
Normal file
7
Task/Y-combinator/Sidef/y-combinator.sidef
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
var y = ->(f) {->(g) {g(g)}(->(g) { f(->(*args) {g(g)(args...)})})};
|
||||
|
||||
var fac = ->(f) { ->(n) { n < 2 ? 1 : (n * f(n-1)) } };
|
||||
say 10.of { |i| y(fac)(i) };
|
||||
|
||||
var fib = ->(f) { ->(n) { n < 2 ? n : (f(n-2) + f(n-1)) } };
|
||||
say 10.of { |i| y(fib)(i) };
|
||||
17
Task/Y-combinator/Swift/y-combinator-1.swift
Normal file
17
Task/Y-combinator/Swift/y-combinator-1.swift
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
struct RecursiveFunc<F> {
|
||||
let o : RecursiveFunc<F> -> F
|
||||
}
|
||||
|
||||
func Y<A, B>(f: (A -> B) -> A -> B) -> A -> B {
|
||||
let r = RecursiveFunc<A -> B> { w in f { w.o(w)($0) } }
|
||||
return r.o(r)
|
||||
}
|
||||
|
||||
let fac = Y { (f: Int -> Int) in
|
||||
{ $0 <= 1 ? 1 : $0 * f($0-1) }
|
||||
}
|
||||
let fib = Y { (f: Int -> Int) in
|
||||
{ $0 <= 2 ? 1 : f($0-1)+f($0-2) }
|
||||
}
|
||||
println("fac(5) = \(fac(5))")
|
||||
println("fib(9) = \(fib(9))")
|
||||
5
Task/Y-combinator/Swift/y-combinator-2.swift
Normal file
5
Task/Y-combinator/Swift/y-combinator-2.swift
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
func Y<A, B>(f: (A -> B) -> A -> B) -> A -> B {
|
||||
typealias RecursiveFunc = Any -> A -> B
|
||||
let r : RecursiveFunc = { (z: Any) in let w = z as! RecursiveFunc; return f { w(w)($0) } }
|
||||
return r(r)
|
||||
}
|
||||
3
Task/Y-combinator/Swift/y-combinator-3.swift
Normal file
3
Task/Y-combinator/Swift/y-combinator-3.swift
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
func Y<In, Out>( f: (In->Out) -> (In->Out) ) -> (In->Out) {
|
||||
return { x in f(Y(f))(x) }
|
||||
}
|
||||
13
Task/Y-combinator/Wart/y-combinator.wart
Normal file
13
Task/Y-combinator/Wart/y-combinator.wart
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def (Y improver)
|
||||
((fn(gen) gen.gen)
|
||||
(fn(gen)
|
||||
(fn(n)
|
||||
((improver gen.gen) n))))
|
||||
|
||||
factorial <- (Y (fn(f)
|
||||
(fn(n)
|
||||
(if zero?.n
|
||||
1
|
||||
(n * (f n-1))))))
|
||||
|
||||
prn factorial.5
|
||||
Loading…
Add table
Add a link
Reference in a new issue