Rev | Line | |
---|
[4] | 1 | #ifndef __FIB_H__ |
---|
| 2 | #define __FIB_H__ |
---|
| 3 | #include "../list.h" |
---|
| 4 | #include "../lazy_op.h" |
---|
| 5 | |
---|
| 6 | class FibBase : List<Int> |
---|
| 7 | { |
---|
| 8 | public: |
---|
| 9 | virtual ~FibBase() { } |
---|
| 10 | |
---|
| 11 | virtual Lazy<Int> getHead() { return head; } |
---|
| 12 | virtual LazyList<Int> getTail() { return tail; } |
---|
| 13 | virtual bool isEmpty() { return false; } |
---|
| 14 | |
---|
| 15 | friend LazyList<Int> mkFib(); |
---|
| 16 | friend R<FibBase> Ref::mk<FibBase>(); |
---|
| 17 | private: |
---|
| 18 | FibBase() : List<Int>(), head(suspend((Int)0L)), tail() { } |
---|
| 19 | Lazy<Int> head; |
---|
| 20 | LazyList<Int> tail; |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | LazyList<Int> mkFib() |
---|
| 24 | { |
---|
| 25 | static auto init = [] |
---|
| 26 | { |
---|
| 27 | R<FibBase> fib = Ref::mk<FibBase>(); |
---|
| 28 | LazyList<Int> l = suspendP<List<Int>, FibBase>(fib); |
---|
| 29 | fib->tail = zipWith(LazyOp<Int>::add, l, suspend((Int)1L) <<= l); |
---|
| 30 | return l; |
---|
| 31 | }; |
---|
| 32 | static LazyList<Int> l = init(); |
---|
| 33 | return l; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | typedef R<FibBase> Fib; |
---|
| 38 | |
---|
| 39 | #endif |
---|
| 40 | |
---|
Note: See
TracBrowser
for help on using the repository browser.