The Art Gallery Guardian

Implement a special kind of recurrence relation as a infinite list


A common recurrence has the form , where and are both infinite sequences, and is a finite sequence. . for all positive . This is well defined as long as are in the same ring.

One can use a balanced binary tree to store the entire infinite list, and the time to generate the th element is , where is the density function of .

Using an array would make it , but it is too imperative for our taste, how about we only use list and achieve 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 elements can be done in time and 3 lines of code. The code

Posted by Chao Xu on .
Tags: Haskell.