
Hi Tomasz,
I actually quite like this style. I was able to understand it after
spending some time reading the docs for some of the functions you used.
My problem I guess is being able to write the code this way when the need
arises or even just recognising when and where it's an option, both of which
to me is considerably harder.
Thanks for the tip.
-John
On 1/29/07, Tomasz Zielonka
On Mon, Jan 29, 2007 at 10:10:47PM +1100, John Ky wrote:
I've written some code and was wondering if there was a better way to write it in terms of readability, brevity and/or efficiency.
This version of mergeForest2 should be more efficient. For even better efficiency it should use Data.Sequence for efficient concatenation (instead of ++). I also made it more general. You have to judge readability yourself.
import qualified Data.Map as Map
data ArcData = ArcData { name :: String } deriving (Show, Eq, Ord) -- derive Ord and Eq
mergeForest2 :: (Ord k) => [Tree k] -> Forest k mergeForest2 = map pairToNode . Map.toList . Map.map mergeForest2 . Map.fromListWith (++) . map nodeToPair where nodeToPair (Node x y) = (x, y) pairToNode = uncurry Node
Best regards Tomasz