The only way I can do this is to create a lazy recursive list and then convert it to an unboxed vector.
Standard puzzling spell
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
will give you an endless list of Fibonacci numbers. Then you can create a list from a list that will have a quick index after that.
Of course, in fact, he does not use the vector to calculate Fibonacci numbers. But it does not index O (n) by linked list, so it should be fast enough.
source
share