Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created February 5, 2013 21:20
Show Gist options
  • Save Sgeo/4717756 to your computer and use it in GitHub Desktop.
Save Sgeo/4717756 to your computer and use it in GitHub Desktop.
Demonstration of mutually recursive tail-call optimized functions in Scala.
/* code taken verbatim from http://www.scala-lang.org/api/current/index.html#scala.util.control.TailCalls$ */
def isEven(xs: List[Int]): TailRec[Boolean] =
if (xs.isEmpty) done(true) else tailcall(isOdd(xs.tail))
def isOdd(xs: List[Int]): TailRec[Boolean] =
if (xs.isEmpty) done(false) else tailcall(isEven(xs.tail))
@Sgeo
Copy link
Author

Sgeo commented Feb 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment