I thought this was pretty funny. Let’s follow the development of one part of my toy bootstrap script. (You have to keep in mind that I’m playing around with functional programming for the first time and that I’m completely utterly horrible at this!) So far, using pseudorandom draws with replacement from a list of data, my script will produce bootstrap replicates of that list. This is the part that, after the random draws have been partitioned into lists of sufficient length goes on to pull out the right elements of the data. The first iteration (heh) goes like this with explicit recursion:
applyShuffle x shuffle = if shuffle == [] then [] else [x !! head shuffle] ++ applyShuffle x (tail shuffle) applyShuffleOrder x shuffleOrder = if shuffleOrder == [] then [] else [applyShuffle x (head shuffleOrder)] ++ applyShuffleOrder x (tail shuffleOrder)
So, a ”shuffle” in the above is a list of indices of elements of data that form a bootstrap replicate. A ”shuffle order” is a list of such lists. Next step, why not try some pattern matching? Also, the (x:xs) notation for lists makes it look a little more Haskell-y:
applyShuffle x [] = [] applyShuffle x (index:indices) = [x !! index] ++ applyShuffle x indices applyShuffleOrder x [] = [] applyShuffleOrder x (shuffle:shuffles) = [applyShuffle x shuffle] ++ applyShuffleOrder x shuffles
Enter the map function! Map really just applies a function recursively to a list. (For friends of R: it’s like llply or lapply.) It’s still recursion, but it kind of reads like what I would say if I was to tell somebody what the function does: ”map the applyShuffle function to the shuffles we’ve already generated”. This is the second function again, but with map:
applyShuffleOrder x shuffles = map f shuffles where f = applyShuffle x
At this point you might realise that the functions can be easily combined into something much shorter. That is, you dear reader might have realised that before, but for me it came at this point. I also decided to stop talking about shuffle orders, and simply call them shuffles. At the end, this is all that remains of the above functions.
applyShuffles x shuffles = map applyShuffle shuffles where applyShuffle = map (x !!)