The Link Class class Link: You should not assume empty the representation here Rest defaults to def __init__(self,first,rest=empty): the empty list assert rest is Link.empty or isinstance(rest,Link) self.first first first->Ist[0] self.rest rest rest->Ist[1:] >>> Ink Link(5,Link(6,Link(7))) Ink is Link.empty->not Ist >>Ink.rest.rest.first first gives elements in the list,.rest traverses 7 >>> Ink.rest.rest.rest is Link.empty Compare to empty list True
The Link Class class Link: empty = () def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link) self.first = first self.rest = rest >>> lnk = Link(5, Link(6, Link(7))) >>> lnk.rest.rest.first 7 >>> lnk.rest.rest.rest is Link.empty True Rest defaults to the empty list You should not assume the representation here Compare to empty list .first gives elements in the list, .rest traverses .first -> lst[0] .rest -> lst[1:] lnk is Link.empty -> not lst
You Try: class Link: >>> a Link(1,Link(2,Link(1))) empty ( >>> b Link(3,Link(2,Link(1))) >>> def __init__(self,first, combined Link(a,Link(b)) rest=empty): How would you retrieve the element 3? self.first first self.rest rest 1.combined.rest.first.rest 2.combined.rest.rest.first 3.combined.rest.first.first 4.combined.first.rest.rest 5.combined.first.rest.first
You Try: class Link: empty = () def __init__(self, first, rest=empty): self.first = first self.rest = rest >>> a = Link(1, Link(2, Link(1))) >>> b = Link(3, Link(2, Link(1))) >>> combined = Link(a, Link(b)) How would you retrieve the element 3? 1. combined.rest.first.rest 2. combined.rest.rest.first 3. combined.rest.first.first 4. combined.first.rest.rest 5. combined.first.rest.first
You Try: a Link(1,link(2,link(1))) 2 b Link(3,link(2,link(1))) 3 combined.rest is an arrow to the next link combined.rest.first is an arrow to b combined Link(a,: Link(b.)) combined.rest:first first
You Try: b = Link(3, link(2, link(1))) combined.rest is an arrow to the next link combined.rest.first is an arrow to b 3 2 1 combined = Link(a, Link(b)) 1 2 1 a = Link(1, link(2, link(1))) combined.rest.first.first