Hi,
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
spot
--
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.
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Anybody? thanks
--
View this message in context: http://old.nabble.com/bit-of-help-with-n-ary-please...-tp26258006p26259268.html
I suppose what you mean is the following: I'm trying to replace an
element in an n-ary tree and display the completed tree.
A more precise specification might help.
- is the tree ordered
- are all the elements in the leaves (not internal nodes)
- is the location of the element to be replaced given?
-- if so how?
- is the tree supposed to be balanced?
On Sun, 8 Nov 2009 15:40:03 -0800 (PST), you wrote:
>
>
>Anybody? thanks
>
>
>
>
>spot135 wrote:
>>
>>
>> Hi,
>>
>> 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.
>>
<snip>
--
Regards,
Casey
replace x y tree
should give a tree with the same structure, values equal to x should be replaced by y, all
other values left as they are?
>
> replace :: Eq a=>a->a->Tree a -> (Tree a)
>
> replace x y Empty = Element x
That should surely be
replace _ _ Empty = Empty ?
>
> replace x y (Leaf z)
>
> | x==z = (Leaf y)
>
> -- | otherwise =
| otherwise = Leaf z
>
> replace x y (Branch z lis)
>
> | x==z = (Branch y lis)
> |otherwise = replaceA x y clis
You'd want to recur, I believe, use
map (replace x y)
>
> 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
>
> spot
_______________________________________________
Hi Casey,
Answers to spec.
the tree is not ordered, it holds string varying in length.
the element value is given, but not its location ie (replace "Two" "Five"
Tree a) would find the location of element "Two" and replace it with "Five"
the tree is not balanced, Nodes can have other Nodes coming off them as well
as Leaves, the number of leaves per node vary as well.
Is that ok?
--
View this message in context: http://old.nabble.com/bit-of-help-with-n-ary-please...-tp26258006p26259580.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________
You didn't answered the "homework" question:
>> I don't know if this is homework.
I'm a bit suspicious: http://www.dcs.shef.ac.uk/intranet/teaching/modules/level2/com2010.html
.
See http://www.haskell.org/haskellwiki/Homework_help .
Cheers,
Stefan
This is the typical bottled response to people asking for homework
help. But spot135 has done a good job so far: stated his problems,
shown his work so far, asking for guidance not answers. And the
community has done a good job helping: no answers, but hints and
leading questions. I would say, so far, this has been a "textbook
example" of a homework exchange.
Luke
> So this is what ive got so far.
>
> data Tree a = Empty | Leaf a | Branch a [Tree a]
[Homework mode on, i.e. hints and careful nudges to help you work it out
on your own]
The definition above gives two ways to build a Tree with only one data
element. Can you see which ones? Is this a good thing?
What is the difference between your definition and
data Tree a = Empty | Branch a [Tree a]
What would the consequences be if you replaced your definition with
this one?
-k
--
If I haven't seen further, it is by standing in the footprints of giants
> This is the typical bottled response to people asking for homework
> help. But spot135 has done a good job so far: stated his problems,
> shown his work so far, asking for guidance not answers. And the
> community has done a good job helping: no answers, but hints and
> leading questions. I would say, so far, this has been a "textbook
> example" of a homework exchange.
Agreed. But still... I think that one (if that's the case) should
clearly indicate that their question relates to homework.
[No desire to fork this thread into an extensive discussion on
homework help, though.]
Cheers,
Stefan
> data Tree a = Empty | Branch a [Tree a]
> What would the consequences be if you replaced your definition with
> this one?
And, for extra credit, can you identify similar issues with this
definition? Can you improve on it?