data TreeL a = EmptyTree 
             | LeafTree a
             | MakeTree [TreeL a] a
   deriving(Show)


-- mapOnSnd e' l'estensione della funzione map
-- a funzioni di tre argomenti in cui il primo ed
-- il secondo argomento sono sempre gli stessi.

mapOnSnd f a1 [] a2 = []

mapOnSnd f a1 (x:xs) a2 = (f a1 x a2):(mapOnSnd f a1 xs a2)




innesta label EmptyTree t = EmptyTree

innesta label  (LeafTree x) t | x== label = MakeTree [t] x

                              | otherwise = (LeafTree x)

innesta label (MakeTree ts x) t | x == label = (MakeTree (t:newchildren) x)

                                | otherwise  = (MakeTree  newchildren x)

                          where newchildren = (mapOnSnd innesta label ts t)

