I have a task with infinite lists.
I have to write a zipWith/3 for infinite lists - done
I have to use this zipWith/3 to create an infinite list of fibonacci numbers with fib/0 - problem
I have to write fibs(N) taking the first N elements from fib() - done
This is what I have so far:
-module(zipWith).
-export([take/2, zipWith/3, fib/0]).
take(0, _) -> [];
take(N, [H|LazyT]) -> [H | take(N-1, LazyT())].
zipWith(F, [H1|L1], [H2|L2]) -> [F(H1, H2) | fun() -> zipWith(F, L1(), L2()) end].
fib() -> ...
fib(L) -> zipWith(fun(X,Y) -> X + Y end, L(), tl(L())).
fibs(N) -> take(N, fib()).
I know fib/1 should look like this (I am pretty sure - correct me, if I am mistaken). Taking the list itself and the list without the head. So in case of [0,1,...] zipWith(add,[0,1,...],[1,...]) results in adding the last two numbers. But whatever I try as the beginning to this fib()->... results in errors. I want to express it somehow like this: fib() -> fib([[0,1] ++ fun() -> ... end]...)
I somehow wanted to start fib/1 with [0,1,fun()...] but do not get how to let the list begin.
Thanks in advance for advice
