Last active
August 29, 2015 14:16
-
-
Save r-wheeler/fd3c8c5b0dc4db792e11 to your computer and use it in GitHub Desktop.
zip strings with tail recursion
This file contains hidden or 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
| 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