|
|
|
|
|
by jfmengels1
1578 days ago
|
|
The mapHelper version that was rejected changed the order. The one that did `List.reverse` had the correct order, and this one is equivalent to the List.foldr solution. The only difference is List.foldr's implementation is more performant (up to a certain number of elements as you say). If this is not what you asked, then I did not understand the question. |
|
First you create an accumulator version of map that reverses the result:
> It’s a bit more verbose, but it’s not that bad. Honestl… Sorry, what did you say? Wrong order? Oh, right…
Then you modify that version to accumulate a reversed result, but reverse it (into the correct order) before returning it:
> we’ll just add a small List.reverse on the accumulator at the very end… It will hurt performance quite a bit but correctness is more important.
Then you reject that solution:
> Surely we can do better than this, right?! Let’s take a look at how List.map is implemented in the core library.
> The order is right because the list is being visited in reverse order (that’s what foldr does, in contrast to foldl). But we need to look at the implementation of List.foldr to see if we can reuse the same idea for our custom functioOH GOD CLOSE THE TAB!
You then complain that the implementation of foldr is too complex to generalize. But it's the same thing you've already explained: accumulate the result, but reverse the list so that when you report the result it's in the right order. This will easily generalize to any other function you care to write.
The standard progression, by the way, as other comments in the thread have pointed out, goes from a recursive solution, to a tail-recursive solution, to a tail-recursive solution with an accumulator, to a tail-recursive solution written in continuation-passing style. Continuations provide the structure to hold your "holes" until they can be filled in. Why didn't you mention this?