Last active
August 29, 2015 14:01
-
-
Save berngp/56e56881132d593e9e20 to your computer and use it in GitHub Desktop.
Fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib: Stream[Long] = { | |
def tail(h: Long, n: Long): Stream[Long] = h #:: tail(n, h + n) | |
tail(0, 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from scala.collection.immutable.Stream http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Stream | |
import scala.math.BigInt | |
object Main extends App { | |
val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 } | |
fibs take 5 foreach println | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment