Continuing my work on immutable arrays in Swarm, this week I was playing around with creating some challenge scenarios that require the use of arrays. This is actually somewhat tricky, since you can always simulate arrays with functions—it's just less efficient. So the general idea is to construct scenarios with a time limit, where you need the efficiency of an array to be able to get a solution that is fast enough. But even this is tricky!
My first attempt just involved a row of randomly generated capital letters that you are supposed to sort alphabetically. I thought you could read them all into an array and pick them up at the same time, sort the array, then lay them out in sorted order. However, I realized that you can just pick them all up, then iterate through the capital letters one at a time in order, and place any that you happen to have of each.
I constructed another scenario that I thought would work: there is a "key" row of randomly permuted capital letters, and another row of randomly generated capital letters. You must sort the second row, but in the order specified by the permutation in the first row. However, I realized there's a clever way to accomplish this in O(n) time without using an array: (1) pick up all the items to be sorted; (2) scan through the key permutation, going one recursive call deeper with each item, so the recursion stack ends up storing the entire sequence; (3) unwind the recursion stack while walking along the destination row in reverse; for each item of the permutation, place all the copies of it you have in your inventory.
Having written this, I think I should go back to my original idea, but use randomly generated strings instead of individual capital letters. That way, the range of possible values is huge, so there's no way to simply iterate through them in order and place any that you happen to have. (Currently, there's no way to iterate through items in your inventory...)