list - Tree: Optional number of subtress -


i have data structure in haskell lets me build tree.

data multtree b = datanode b | indexnode int int (multtree b) (multtree b) (multtree b) deriving (show) 

in case possible have indexnode needs 3 multtree's parameters.

how can make indexnode able receive 0, 1, 2 or 3 multtree's ? implementing indexnode different number of parameters not seem work.

so in end create tree that:

t2 :: multtree int t2 = indexnode 3 42 (indexnode 3 15 (3) (11) (12)) (indexnode 19 42 (42) (23)) 

define own type containing 0 3 things:

data from0to3 = 0 | 1 | 2 a | 3 a    deriving (show) data multtree b = datanode b | indexnode int int (from0to3 (multtree b))    deriving (show)  t2 :: multtree int t2 = indexnode 3 42       (two (indexnode 3 15               (three (datanode 3) (datanode 11) (datanode 12)))           (indexnode 19 42              (two (datanode 42) (datanode 23)))) 

as requested, here's how dissect such tree. instance, following computes height of tree.

height :: multtree -> int height (datanode _)         = 1 height (indexnode _ _ zero) = 1 height (indexnode _ _ (one t1)) =    1 + height t1 height (indexnode _ _ (two t1 t2)) =    1 + (height t1 `max` height t2) height (indexnode _ _ (three t1 t2 t3)) =    1 + (height t1 `max` height t2 `max` height t3) 

when writing kind of pattern matching, recommend turn on warnings (-wall) ghc tell if forget handle case.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -