Skip to content

Instantly share code, notes, and snippets.

@berngp
Last active August 29, 2015 14:01
Show Gist options
  • Save berngp/56e56881132d593e9e20 to your computer and use it in GitHub Desktop.
Save berngp/56e56881132d593e9e20 to your computer and use it in GitHub Desktop.
Fibonacci
def fib: Stream[Long] = {
def tail(h: Long, n: Long): Stream[Long] = h #:: tail(n, h + n)
tail(0, 1)
}
//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