# Implement a special kind of recurrence relation as a infinite list

A common recurrence has the form $a_{n}={∑_{i=0}b_{i}a_{n−m_{i}}c_{n} ifn>kifn≤k $ , where $m_{i}$ and $b_{i}$ are both *infinite* sequences, and
$c_{i}$ is a finite sequence. $m_{i}∈N$. $a_{−i}=0$ for all positive $i$. This is well defined as long as $b_{i},a_{i},c_{i}$ are in the same ring.

One can use a balanced binary tree to store the entire infinite list, and the time to generate the $n$th element is $O(d(n)gn)$, where $d$ is the density function of ${m_{i}}$.

Using an array would make it $O(d(n))$, but it is too imperative for our taste, how about we only use list and achieve $O(d(n))$ time, elegantly?

The idea is that we are summing the first item of infinite many stacks. However we don’t have to really sum the infinite stacks, we only sum the stack we require. The code:

What’s a practical usage of this? Produce a infinite list of partition numbers, such that finding the first $n$ elements can be done in $O(n_{23})$ time and 3 lines of code. The code