Here’s a fun kata:
blendfunction that, given two lists of the same length, returns a new list with each element alternated. E.g.:
blend [1, 2, 3] [4, 5, 6] => [1, 4, 2, 5, 3, 6]
As with all challenges, it can be solved in many different ways. However this particular one is easily solvable with functional programming techniques such as recursion.
You can try implementing it on your own first or just look straight at the solutions below.
Solution in Elm
The one below is probably the most straightforward solution:
Notice how I exchanged the arguments in the recursion call. That did the trick!
Let’s try it in the REPL – I added slashes so you can copy-paste the function:
Solution in Swift
We can achieve the same in Swift by using an extension that splits up an Array into head and tail (credits to Chris Eidhof):
And here’s the solution:
If you know of a better way, please let me know! Also feel free to leave a comment with any other alternative solution, even in other languages.