Skip to content

Instantly share code, notes, and snippets.

@r-wheeler
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save r-wheeler/fd3c8c5b0dc4db792e11 to your computer and use it in GitHub Desktop.

Select an option

Save r-wheeler/fd3c8c5b0dc4db792e11 to your computer and use it in GitHub Desktop.
zip strings with tail recursion
impprt scala.annotation.tailrec
def f(a: String, b: String): String = {
@tailrec
def loop(a: String, b: String, acc: String): String = {
if (a.length == 0 && b.length ==0 ) acc
else {
val (al, ar) = a.splitAt(1)
val (bl, br) = b.splitAt(1)
val x = al + bl
loop(ar, br, x + acc)}
}
loop(a.reverse,b.reverse,"")
}
f("hacker","ranker")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment