Skip to content

Instantly share code, notes, and snippets.

View tautologico's full-sized avatar

Andrei Formiga tautologico

  • UFPB
  • João Pessoa, Paraíba
View GitHub Profile
@tautologico
tautologico / list.rs
Last active December 26, 2015 14:19
Example of using lists.
// 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)];
@tautologico
tautologico / listbang.rs
Created October 26, 2013 04:05
Example of using the list building macro.
let l = list!(5, 4, 4, 3);
@tautologico
tautologico / listmacro.rs
Created October 26, 2013 03:22
A macro for easier building of lists.
macro_rules! list(
( $e:expr, $($rest:expr),+ ) => ( @Cons($e, list!( $( $rest ),+ )) );
( $e:expr ) => ( @Cons($e, @Empty) );
() => ( @Empty )
)
@tautologico
tautologico / length_acc.rs
Created October 25, 2013 00:46
Tail-recursive length function for lists, using an accumulator.
fn length_acc<A>(l: @List<A>, acc: uint) -> uint {
match *l {
Empty => acc,
Cons(_, rest) => length_acc(rest, acc + 1)
}
}
@tautologico
tautologico / polylen.rs
Last active December 26, 2015 11:49
Polymorphic length function and test.
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));
@tautologico
tautologico / polylist.rs
Last active December 26, 2015 11:49
Polymorphic lists.
enum List<A> {
Empty,
Cons(A, @List<A>)
}
@tautologico
tautologico / intlist.rs
Last active March 12, 2016 23:37
Integer lists in Rust.
// 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)
}
@tautologico
tautologico / main_intlist.rs
Created October 24, 2013 22:15
Test of length and sum for integer lists.
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));
}
@tautologico
tautologico / sumlist.rs
Created October 24, 2013 22:12
Function to sum all the values in a list of integers.
fn sum_list(l: @IntList) -> int {
match *l {
Empty => 0,
Cons(i, rest) => i + sum_list(rest)
}
}
@tautologico
tautologico / intlist_len.rs
Created October 24, 2013 22:05
Function to compute the length of a list in Rust.
fn length(l: @IntList) -> uint {
match *l {
Empty => 0,
Cons(_, rest) => 1 + length(rest)
}
}