Last active
January 4, 2016 21:19
-
-
Save quux00/8679757 to your computer and use it in GitHub Desktop.
Hof in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn double(a: int) -> int { a * 2 } | |
fn ntimes(f: proc(int) -> int, times: int) -> proc(int) -> int { | |
proc(x: int) { | |
match times { | |
0 => { x }, | |
_ => { f(ntimes(f, times - 1)(x)) } // swap the order relative to the original example | |
} | |
} | |
} | |
fn main() { | |
let quadruple = ntimes(double, 3); | |
println!("quad: {:d}", quadruple(3)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment