data NLT a b = Node a (NLT a b) (NLT a b) | Leaf b fold f g (Node a l r) = f a (fold f g l) (fold f g r) fold f g (Leaf b) = g b nls (Leaf b) = ([],[b]) nls (Node a l r) = (a:las++ras, lbs++rbs) where (las,lbs) = nls l (ras,rbs) = nls r nl' = fold (\a (la,lb) (ra,rb) -> (a:la++ra, lb++rb)) (\b->([],[b])) samp = Node 'a' (Node 'b' (Leaf 5) (Leaf 7)) (Node 'c' (Leaf 8) (Node 'd' (Leaf 9) (Leaf 10))) data BT a = Branch a (BT a) (BT a) | Tip foldb b t (Branch x l r) = b x (foldb b t l) (foldb b t r) foldb b t Tip = t inord = foldb (\x l r->l++x:r) [] t = Branch 3 (Branch 1 Tip (Branch 2 Tip Tip)) (Branch 5 (Branch 4 Tip Tip) (Branch 7 Tip (Branch 9 Tip Tip)) ) ---------- ins (x,i) Tip = Branch (x,[i]) Tip Tip ins (x,i) (Branch (y,is) l r) | x==y = Branch (y,i:is) l r | xy = Branch (y,is) l (ins (x,i) r) conc t = inord (foldr ins Tip (zip (words t) [1..])) ---------- text = "this a text with some words in it and some of the words appear twice \ \and we are going to use only letters since it is hard to handle \ \things like commas so this text of ours will run to many words \ \and many of those words will be used twice such as words text in is it \ \and so on" fmt :: Show a => [(String,[a])] -> IO() fmt = putStrLn . concatMap line where line (word, ints) = concat ["\n", word, "\t", comma (map show ints)] comma = foldr1 (\i str -> i ++ ", " ++ str)