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
// Polymorphic lists | |
// Compile with "rustc -o list list.rs" | |
// more recent versions of the compiler need this for @ pointers | |
#[feature(managed_boxes)]; | |
// enable macros | |
#[feature(macro_rules)]; | |
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
let l = list!(5, 4, 4, 3); |
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
macro_rules! list( | |
( $e:expr, $($rest:expr),+ ) => ( @Cons($e, list!( $( $rest ),+ )) ); | |
( $e:expr ) => ( @Cons($e, @Empty) ); | |
() => ( @Empty ) | |
) |
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 length_acc<A>(l: @List<A>, acc: uint) -> uint { | |
match *l { | |
Empty => acc, | |
Cons(_, rest) => length_acc(rest, acc + 1) | |
} | |
} |
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 length<A>(l: @List<A>) -> uint { | |
match *l { | |
Empty => 0, | |
Cons(_, rest) => 1 + length(rest) | |
} | |
} | |
fn main() { | |
let l = @Cons("3", @Cons("little", @Cons("strings", @Empty))); | |
println!("Length of l is {}", length(l)); |
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
enum List<A> { | |
Empty, | |
Cons(A, @List<A>) | |
} |
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) | |
} |
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 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)); | |
} |
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 sum_list(l: @IntList) -> int { | |
match *l { | |
Empty => 0, | |
Cons(i, rest) => i + sum_list(rest) | |
} | |
} |
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 length(l: @IntList) -> uint { | |
match *l { | |
Empty => 0, | |
Cons(_, rest) => 1 + length(rest) | |
} | |
} |