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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -