[Haskell-cafe] bit of help with n-ary please...

spot135 aca08sas at shef.ac.uk
Sun Nov 8 17:24:59 EST 2009


Ok what im trying to do is replace an element in an n-ary and display the
completed tree.

So this is what ive got so far.

data Tree a = Empty |Leaf a|Branch a [Tree a]
				deriving (Show)

replace :: Eq a=>a->a->Tree a -> (Tree a)

replace x y Empty = Element x

replace x y (Leaf z)
	| x==z = (Leaf y)
--	| otherwise = 
replace x y (Branch z lis)
	| x==z = (Branch y lis)
	|otherwise = replaceA x y clis

replaceA :: Eq a=> a->a->[Tree a]->(Tree a)
replaceA _ _ [] = Empty -- run out
replaceA x y (h:r)
	|Q suc = suc 
	|otherwise = replaceA x y r 
	where suc=replace x y h 
Q :: a -> Bool
Q a = True
Q a = False

Theres two main problems i am having. One is I'm not sure what should be the
"otherwise" in the leaf function. Another problem is it doesn't show the
whole tree just the part after the replacement.

If you think this is complete bull I'll quite happily start over if somebody
could give me some insight 
into what is the best way of tackling this problem is.

Many thanks in advance


View this message in context: http://old.nabble.com/bit-of-help-with-n-ary-please...-tp26258006p26258006.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list