3 ms·
Those are some good examples of using the Par monad. I was once asked during an interview to transform a tree into a linked list. The interview went pretty wel
by wyager 12y ago
Those are some good examples of using the Par monad.
I was once asked during an interview to transform a tree into a linked list. The interview went pretty well, but immediately after leaving, I was struck by l'esprit de l'escalier and this nice one-liner (featured in the article) popped into my head:
toList = foldr (:) []
It's the identity function on lists, but it can actually transform any Foldable into a list as well, and it's very efficient. O(n) for trees, very little overhead, and lazy. I believe it's used in a number of Haskell containers.
- tel 12y agoThat's actually implemented as part of Data.Foldable [0]. Super canonical! [0] http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Foldable.html#v:toList http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Fo...
- nandemo 12y agoNice, but if I were the interviewer I would expect the candidate to also implement foldr for Tree, since that's the core of the problem.