You should call rlist()
consistently, and write
elif k == 1:
return rlist(curr, empty_rlist)
... for reasons that will become apparent. Also be sure to use four spaces per level of indentation, as prescribed by PEP 8.
Are your tuple, range, and string the same as each other? No, comparing them using ==
will obviously return False
. What they all have in common, though, is that they are all iterables. Being iterable means that you can use them in for e in iterable
expressions — either in a loop or a generator.
That means that if you want to be able to write things like print([e for e in fib_sequence(5)])
, you'll need to make your rlist
type iterable. Here's one way you could accomplish that.
class rlist(tuple):
def __new__(cls, first, rest):
return super(rlist, cls).__new__(cls, tuple([firstfirst, rest]rest))
def __iter__(self):
seq = self
while Trueseq is not None:
yield first(seq)
seq = rest(seq)
if seq is None:
break
This works as long as you have applied the rlist()
fix to fib_sequence()
as mentioned above.
You should call rlist()
consistently, and write
elif k == 1:
return rlist(curr, empty_rlist)
... for reasons that will become apparent. Also be sure to use four spaces per level of indentation, as prescribed by PEP 8.
Are your tuple, range, and string the same as each other? No, comparing them using ==
will obviously return False
. What they all have in common, though, is that they are all iterables. Being iterable means that you can use them in for e in iterable
expressions — either in a loop or a generator.
That means that if you want to be able to write things like print([e for e in fib_sequence(5)])
, you'll need to make your rlist
type iterable. Here's one way you could accomplish that.
class rlist(tuple):
def __new__(cls, first, rest):
return super(rlist, cls).__new__(cls, tuple([first, rest]))
def __iter__(self):
seq = self
while True:
yield first(seq)
seq = rest(seq)
if seq is None:
break
This works as long as you have applied the rlist()
fix to fib_sequence()
as mentioned above.
You should call rlist()
consistently, and write
elif k == 1:
return rlist(curr, empty_rlist)
... for reasons that will become apparent. Also be sure to use four spaces per level of indentation, as prescribed by PEP 8.
Are your tuple, range, and string the same as each other? No, comparing them using ==
will obviously return False
. What they all have in common, though, is that they are all iterables. Being iterable means that you can use them in for e in iterable
expressions — either in a loop or a generator.
That means that if you want to be able to write things like print([e for e in fib_sequence(5)])
, you'll need to make your rlist
type iterable. Here's one way you could accomplish that.
class rlist(tuple):
def __new__(cls, first, rest):
return super(rlist, cls).__new__(cls, (first, rest))
def __iter__(self):
seq = self
while seq is not None:
yield first(seq)
seq = rest(seq)
This works as long as you have applied the rlist()
fix to fib_sequence()
as mentioned above.
You should call rlist()
consistently, and write
elif k == 1:
return rlist(curr, empty_rlist)
... for reasons that will become apparent. Also be sure to use four spaces per level of indentation, as prescribed by PEP 8.
Are your tuple, range, and string the same as each other? No, comparing them using ==
will obviously return False
. What they all have in common, though, is that they are all iterables. Being iterable means that you can use them in for e in iterable
expressions — either in a loop or a generator.
That means that if you want to be able to write things like print([e for e in fib_sequence(5)])
, you'll need to make your rlist
type iterable. Here's one way you could accomplish that.
class rlist(tuple):
def __new__(cls, first, rest):
return super(rlist, cls).__new__(cls, tuple([first, rest]))
def __iter__(self):
seq = self
while True:
yield first(seq)
seq = rest(seq)
if seq is None:
break
This works as long as you have applied the rlist()
fix to fib_sequence()
as mentioned above.