|
|
@ -4,7 +4,6 @@ pub enum List<A> {
|
|
4
|
4
|
Cons(A, Box<List<A>>)
|
|
5
|
5
|
}
|
|
6
|
6
|
|
|
7
|
|
// xs: doesn't work as a reference, probably as Box?
|
|
8
|
7
|
pub fn cons<A>(x: A, xs: List<A>) -> List<A> {
|
|
9
|
8
|
match xs {
|
|
10
|
9
|
Nil => Cons(x, box Nil),
|
|
|
@ -12,22 +11,16 @@ pub fn cons<A>(x: A, xs: List<A>) -> List<A> {
|
|
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
|
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
|
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
|
25
|
println!("nil = {}, ns = {}", nil, ns);
|
|
33
|
26
|
println!("first(nil) = {}, first(ns) = {}", first(nil), first(ns));
|