2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,27 +1,24 @@
|
|||
use std::sync::Arc;
|
||||
use std::boxed::Box;
|
||||
use std::clone::Clone;
|
||||
|
||||
//Arc<Box<Closure>>
|
||||
#[macro_export]
|
||||
macro_rules! abc {
|
||||
($x:expr) => (Arc::new(Box::new($x)));
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Mu<T> {
|
||||
enum Mu<T> {
|
||||
Roll(Arc<Box<Fn(Mu<T>)->T>>),
|
||||
}
|
||||
|
||||
pub fn unroll<T>(Mu::Roll(f): Mu<T>) -> Arc<Box<Fn(Mu<T>)->T>> {f.clone()}
|
||||
fn unroll<T>(Mu::Roll(f): Mu<T>) -> Arc<Box<Fn(Mu<T>)->T>> {f.clone()}
|
||||
|
||||
pub type Func<A, B> = Arc<Box<Fn(A)->B>>;
|
||||
pub type RecFunc<A, B> = Arc<Box<Fn(Func<A, B>) -> Func<A, B>>>;
|
||||
pub type Func<A> = Arc<Box<Fn(A)->A>>;
|
||||
pub type RecFunc<A> = Arc<Box<Fn(Func<A>) -> Func<A>>>;
|
||||
|
||||
pub fn y<A, B>(f: RecFunc<A, B>) -> Func<A, B> {
|
||||
let g:Arc<Box<Fn(Mu<Func<A, B>>)->Func<A, B>>> = abc!(move |x : Mu<Func<A, B>>| -> Func<A, B> {
|
||||
pub fn y<A: 'static>(f: RecFunc<A>) -> Func<A> {
|
||||
let g: Arc<Box<Fn(Mu<Func<A>>)->Func<A>>> = abc!(move |x : Mu<Func<A>>| -> Func<A> {
|
||||
let f = f.clone();
|
||||
abc!(move |a:A| -> B {
|
||||
abc!(move |a:A| -> A {
|
||||
let f = f.clone();
|
||||
f(unroll(x.clone())(x.clone()))(a)
|
||||
})
|
||||
|
|
@ -29,16 +26,24 @@ pub fn y<A, B>(f: RecFunc<A, B>) -> Func<A, B> {
|
|||
g(Mu::Roll(g.clone()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fib_test() {
|
||||
let fib : RecFunc<i32, i32> = abc!(|f| abc!(move |x| if (x<2) { 1 } else { f(x-1) + f(x-2)}));
|
||||
let b = y(fib)(10);
|
||||
assert_eq!(b, 89);
|
||||
#[macro_export]
|
||||
macro_rules! y {
|
||||
(|$name:ident| $fun:tt) => {
|
||||
y(abc!(|$name| abc!($fun)))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fac_test() {
|
||||
let fac : RecFunc<i32, i32> = abc!(|f| abc!(move |x| if (x==0) { 1 } else { f(x-1) * x }));
|
||||
let c = y(fac)(10);
|
||||
assert_eq!(c, 3628800);
|
||||
fn fac(n: u32) -> u32 {
|
||||
let fn_: Func<u32> = y!(|f| (move |x| if x == 0 { 1 } else { f(x-1) * x }));
|
||||
fn_(n)
|
||||
}
|
||||
|
||||
fn fib(n: u32) -> u32 {
|
||||
let fn_: Func<u32> = y!(|f| (move |x| if x < 2 { x } else { f(x-1) + f(x-2) }));
|
||||
fn_(n)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", fac(10));
|
||||
println!("{}", fib(10))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue