I’d like some advice, either directly or referral to book/website that has some good material on defining types.
I’m implementing a red-black tree and was wondering whether coding the colour into the type is going too far … is a node either RED or BLK or does a node have a RED or BLK property. Six of one probably in this case …
type Tree<'a> =
| Blk of Tree<'a> * 'a * Tree<'a>
| Red of Tree<'a> * 'a * Tree<'a>
| Nil
or do this
type Colour = RED | BLK
type Tree<'a> =
| T of Colour * Tree<'a> * 'a * Tree<'a>
| Nil
I implemented using the first option, and was pleasantly surprised at how easy it was to refactor to the second, and even better, it worked first time! I prefer the look of the second option code as it looks more concise - maybe that’s the simple answer?
Thanks.