Tue, 27 Feb 2001 13:26:44 +0100
Martin Gustafsson schrieb folgendes am Mon, Feb 26, 2001 at 03:05:50PM +0100:
> I'm a haskell newbie that tries to create a tree with arbitary numbers of childs.
> I create the data structure but i can't do anything on it can someone please help
> me with a small function that sums the values of the leafs, so i donīt loose my hair
> so fast.
> The datastructure looks like this and a binary tree built with it would look like this:
> data GeneralTree = Nil | Node (Integer,[GeneralTree])
treeSum Nil = 0
treeSum Node (s, subtrees) = s + sum (map treeSum subtrees)
More general you may define
data GTree a = Nil | Node a [Gtree a]
accumTree : (a -> b -> b) -> b -> Gtree a -> b
accumTree f init Nil = init
accumTree f init Node x ts = let rs = map accumTree f init ts
r = foldl f init rs
in foldl f init [x, r]
sumTree = accumTree + 0
You can't push on a rope.