reset password
Author Message
rabbott
Posts: 1649
Posted 19:29 Nov 20, 2015 |

In the Tuesday lecture, when I expanded Fibonacci on the board, the initial numbers came out as 0 1 2 3 5 ... . The second "1" was missing. I don't know what I did wrong. The code on the wiki page comes out right. To run it you need both stream-map-n and stream-take, which are also defined on the page.

> (define fibonacci
    (stream-cons 0 (stream-cons 1 (stream-map-n + fibonacci (stream-rest fibonacci)))))
> (stream-take fibonacci 8)
'
(0 1 1 2 3 5 8 13)

 

rabbott
Posts: 1649
Posted 15:55 Nov 21, 2015 |

Here's what I did wrong. I showed that 

(stream-map-n + fibonacci (stream-rest fibonacci)))

the sum of the fibonacci sequence with it's tail is: '(1 2 3 5 8 13). (In class I made a mistake and put a 0 in front.) 

    '(0 1 1 2 3 5 8 13 ...)   -- fibonacci

  + '(1 1 2 3 5 8 13 ...)     -- (stream-rest fibonacci)

---------------------

    '(1 2 3 5 8 13 ...) 

The fibonacci function returns that sum with '(0 1) put in front, making the whole thing the correct fibonacci sequence:

(stream-cons 0 (stream-cons 1 '(2 3 5 8 13 ...)) -> '(0 1 1 2 3 5 8 13 ...)