junkcode
Check-in [831facee98]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Get working example going.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:831facee98ff4e63c4a8aa09ec232a83769766385d1a5d64419630c022e8b53c
User & Date: jaccarmac 2017-12-31 06:59:53
Context
2018-01-07
08:19
cargo update again check-in: 9f540cc944 user: jaccarmac tags: trunk
2017-12-31
06:59
Get working example going. check-in: 831facee98 user: jaccarmac tags: trunk
06:42
Remove nested data structure. check-in: cc63b07abf user: jaccarmac tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to polymorphic-recursion/rust/src/main.rs.

1
2



3
4


5



6
7
8
9
10

11


















12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27












enum IntList {
    Nil,



    Cons(i64, Box<IntList>),
}






impl IntList {
    pub fn length(self) -> i64 {
        match self {
            IntList::Nil => 0,
            IntList::Cons(_, t) => 1 + t.length(),

        }


















    }
}

fn main() {
    let int_list = IntList::Cons(
        0,
        Box::new(IntList::Cons(
            1,
            Box::new(IntList::Cons(
                2,
                Box::new(IntList::Cons(3, Box::new(IntList::Nil))),
            )),
        )),
    );
    println!("{}", int_list.length());
}












|
|
>
>
>
|
|
>
>
|
>
>
>
|
|
<
<
<
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|

|

|

|




|
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
trait IntList {
    fn length(&self) -> i64;
}

struct IntListNil;
struct IntListCons(i64, Box<IntList>);

impl IntList for IntListNil {
    fn length(&self) -> i64 {
        0
    }
}

impl IntList for IntListCons {
    fn length(&self) -> i64 {



        1 + self.1.length()
    }
}

trait Nested<T> {
    fn length(&self) -> i64;
}

struct NestedEpsilon;
struct NestedCons<T>(T, Box<Nested<Vec<T>>>);

impl<T> Nested<T> for NestedEpsilon {
    fn length(&self) -> i64 {
        0
    }
}

impl<T> Nested<T> for NestedCons<T> {
    fn length(&self) -> i64 {
        1 + self.1.length()
    }
}

fn main() {
    let int_list = IntListCons(
        0,
        Box::new(IntListCons(
            1,
            Box::new(IntListCons(
                2,
                Box::new(IntListCons(3, Box::new(IntListNil))),
            )),
        )),
    );
    println!("{}", int_list.length());

    let nested = NestedCons::<i64>(
        0,
        Box::new(NestedCons::<Vec<i64>>(
            vec![1, 2],
            Box::new(NestedCons::<Vec<Vec<i64>>>(
                vec![vec![3], vec![4]],
                Box::new(NestedEpsilon),
            )),
        )),
    );
    println!("{}", nested.length());
}