Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1 view
Skip to first unread message

spot135

unread,
Nov 8, 2009, 5:25:19 PM11/8/09
to haskel...@haskell.org

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

spot135

unread,
Nov 8, 2009, 6:40:24 PM11/8/09
to haskel...@haskell.org

Anybody? thanks

--
View this message in context: http://old.nabble.com/bit-of-help-with-n-ary-please...-tp26258006p26259268.html

Casey Hawthorne

unread,
Nov 8, 2009, 7:06:01 PM11/8/09
to haskel...@haskell.org
I don't know if this is homework.

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

Daniel Fischer

unread,
Nov 8, 2009, 7:16:34 PM11/8/09
to haskel...@haskell.org
Am Sonntag 08 November 2009 23:24:59 schrieb spot135:
> 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 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

_______________________________________________

spot135

unread,
Nov 8, 2009, 7:28:40 PM11/8/09
to haskel...@haskell.org

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.

_______________________________________________

Stefan Holdermans

unread,
Nov 9, 2009, 2:31:28 AM11/9/09
to spot135, haskel...@haskell.org
> Answers to spec.


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

Luke Palmer

unread,
Nov 9, 2009, 2:39:52 AM11/9/09
to Stefan Holdermans, haskel...@haskell.org
On Mon, Nov 9, 2009 at 12:31 AM, Stefan Holdermans <ste...@cs.uu.nl> wrote:
>> Answers to spec.
>
>
> 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 .

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

Ketil Malde

unread,
Nov 9, 2009, 2:42:33 AM11/9/09
to haskel...@haskell.org
spot135 <aca0...@shef.ac.uk> writes:

> 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

Stefan Holdermans

unread,
Nov 9, 2009, 2:45:02 AM11/9/09
to Luke Palmer, haskel...@haskell.org
Luke,

> 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

Ketil Malde

unread,
Nov 9, 2009, 3:04:31 AM11/9/09
to haskel...@haskell.org
Ketil Malde <ke...@malde.org> writes:

> 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?

0 new messages