2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-19 15:59:44 -05:00
|
|
|
// xfail-fast
|
2012-09-18 17:52:21 -05:00
|
|
|
|
2013-03-21 19:29:49 -05:00
|
|
|
fn fix_help<A:'static,B:Owned>(f: extern fn(@fn(A) -> B, A) -> B, x: A) -> B {
|
2013-01-15 18:33:20 -06:00
|
|
|
return f(|a| fix_help(f, a), x);
|
2011-09-23 16:58:06 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 19:29:49 -05:00
|
|
|
fn fix<A:'static,B:Owned>(f: extern fn(@fn(A) -> B, A) -> B) -> @fn(A) -> B {
|
2013-01-15 18:33:20 -06:00
|
|
|
return |a| fix_help(f, a);
|
2011-09-23 16:58:06 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 17:55:31 -06:00
|
|
|
fn fact_(f: @fn(v: int) -> int, n: int) -> int {
|
2011-09-23 16:58:06 -05:00
|
|
|
// fun fact 0 = 1
|
2012-08-01 19:30:05 -05:00
|
|
|
return if n == 0 { 1 } else { n * f(n - 1) };
|
2011-09-23 16:58:06 -05:00
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2011-09-23 16:58:06 -05:00
|
|
|
let fact = fix(fact_);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((fact(5) == 120));
|
|
|
|
fail_unless!((fact(2) == 2));
|
2011-09-23 16:58:06 -05:00
|
|
|
}
|