Last active
March 12, 2016 23:37
-
-
Save tautologico/7146153 to your computer and use it in GitHub Desktop.
Integer lists 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
// Functional-style integer lists | |
// Compile with "rustc -o intlist intlist.rs" | |
// more recent versions of the compiler need this for @ pointers | |
#[feature(managed_boxes)]; | |
enum IntList { | |
Empty, | |
Cons(int, @IntList) | |
} | |
fn length(l: @IntList) -> uint { | |
match *l { | |
Empty => 0, | |
Cons(_, rest) => 1 + length(rest) | |
} | |
} | |
fn sum_list(l: @IntList) -> int { | |
match *l { | |
Empty => 0, | |
Cons(i, rest) => i + sum_list(rest) | |
} | |
} | |
fn main() | |
{ | |
let l1 = @Cons(5, @Cons(3, @Cons(7, @Empty))); | |
println!("length of l1 is {}", length(l1)); | |
println!("sum of l1 is {}", sum_list(l1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment