scala - How to create sequence? -
i'm trying come endless fibonacci sequence of numbers function, passes 2 parameters. parameters set first 2 elements in sequence. can please? thank you
def fib(i: int, j: int): stream[int] = { case 0 | 1 => current case _ => fib( current-1 ) + fib( current -2 ) }
this easy do, however, have recurs in other direction. not define current element based on previous elements function receives current arguments , calls arguments of next value:
def fib(i: int, j: int): stream[int] = #:: fib(j, + j) println(fib(0,1).take(10))
in contrast typical recursive definition, not quaratic linear, quite efficient. (streams of course more complex simple while loop).
Comments
Post a Comment