A colleague has also been starting to learn F#, and today gave me a problem he’d been trying to solve in F# in a “functional style”. He wanted a function that took a string input, and returned a list of the words, tupled with the starting index.
I spent the whole of lunch staring at my screen, trying to wrap my head around List.fold, list.foldBack and other functions; but no joy.
While driving home, it occurred to me that I could do away with that nonsense, and just have a recursive function that passes all the values down, top-to-bottom!
Here’s my latest attempt… The results are correct, which is an improvement over my original attempt!
Can anyone come up with something more elegant?
Update
I managed to simplify/flatten it a little, though I’m not convinced it’s any more readable!