Просмотр исходного кода

don't use references for now.

they will return when i understand them a bit.
Lucas Stadler лет назад: 11
Родитель
Сommit
af9013be8c
1 измененных файлов с 5 добавлено и 12 удалено
  1. 5 12
      rust/list.rs

+ 5 - 12
rust/list.rs

4
    Cons(A, Box<List<A>>)
4
    Cons(A, Box<List<A>>)
5
}
5
}
6
6
7
// xs: doesn't work as a reference, probably as Box?
8
pub fn cons<A>(x: A, xs: List<A>) -> List<A> {
7
pub fn cons<A>(x: A, xs: List<A>) -> List<A> {
9
    match xs {
8
    match xs {
10
        Nil => Cons(x, box Nil),
9
        Nil => Cons(x, box Nil),
12
    }
11
    }
13
}
12
}
14
13
15
pub fn first<A>(xs: &List<A>) -> Option<&A> {
14
pub fn first<A>(xs: List<A>) -> Option<A> {
16
    match xs {
15
    match xs {
17
        &Nil => None,
18
        &Cons(ref x, _) => Some(x)
16
        Nil => None,
17
        Cons(x, _) => Some(x)
19
    }
18
    }
20
}
19
}
21
20
22
// nth
23
// map - does this make sense? (e.g. we probably don't want to copy everything.) should it be lazy?
24
//   fn map(..., &List<&A>) -> List<B> // or -> List<&B>?
25
// filter - we want references to the old values and keep the existing cons cells
26
// reduce
27
28
pub fn main() {
21
pub fn main() {
29
	let nil: &List<int> = &Nil;
30
	let ns:  &List<int> = &cons(1i, cons(2, cons(3, Nil)));
22
	let nil: List<int> = Nil;
23
	let ns:  List<int> = cons(1i, cons(2, cons(3, Nil)));
31
24
32
	println!("nil = {}, ns = {}", nil, ns);
25
	println!("nil = {}, ns = {}", nil, ns);
33
	println!("first(nil) = {}, first(ns) = {}", first(nil), first(ns));
26
	println!("first(nil) = {}, first(ns) = {}", first(nil), first(ns));