Questions About Rose Trees Analysis

Dear All, I am trying to learn Haskell (for the second time), but I have to admit I am still struggling with relative simple stuff, so I ask if anybody can give me a hand. I am dealing with the issues of extracting info out of some rose trees and probably I am also having some troubles with the notation. The rose tree is defined with the following notation data Rose a = a :> [Rose a] deriving (Eq, Show) and the root can be detected simply as root (a :> rs) = a I would like to have the expression (with the ":>" notation for the Node) of the function to find the children of the root. Example of expected behavior of this function children children (1 :> [2 :> [], 3 :> []]) = [2 :> [], 3 :> []] On top of that, I am trying to get the functions to have the functions size :: Rose a -> Int leaves :: Rose a -> Int that count the number of nodes in a rose tree, respectively the number of leaves (nodes without any children). For the children function, I have tried stuff like children (a :> rs) = rs quite unsuccessfully. Any suggestion is appreciated. Cheers Lorenzo

On Thu, 17 Dec 2015 11:37:24 +0100, Lorenzo Isella
data Rose a = a :> [Rose a] deriving (Eq, Show)
and the root can be detected simply as
root (a :> rs) = a
I would like to have the expression (with the ":>" notation for the Node) of the function to find the children of the root. Example of expected behavior of this function children
children (1 :> [2 :> [], 3 :> []]) = [2 :> [], 3 :> []]
On top of that, I am trying to get the functions to have the functions
size :: Rose a -> Int leaves :: Rose a -> Int
that count the number of nodes in a rose tree, respectively the number of leaves (nodes without any children). For the children function, I have tried stuff like
children (a :> rs) = rs
quite unsuccessfully.
: Your definition of children is correct, what is the message you get from the compiler/interpreter? Regards, Henk-Jan van Tuyl -- Folding@home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --

On Thu, Dec 17, 2015 at 12:10:01PM +0100, Henk-Jan van Tuyl wrote:
On Thu, 17 Dec 2015 11:37:24 +0100, Lorenzo Isella
wrote: : data Rose a = a :> [Rose a] deriving (Eq, Show)
and the root can be detected simply as
root (a :> rs) = a
I would like to have the expression (with the ":>" notation for the Node) of the function to find the children of the root. Example of expected behavior of this function children
children (1 :> [2 :> [], 3 :> []]) = [2 :> [], 3 :> []]
On top of that, I am trying to get the functions to have the functions
size :: Rose a -> Int leaves :: Rose a -> Int
that count the number of nodes in a rose tree, respectively the number of leaves (nodes without any children). For the children function, I have tried stuff like
children (a :> rs) = rs
quite unsuccessfully.
:
Your definition of children is correct, what is the message you get from the compiler/interpreter?
Regards, Henk-Jan van Tuyl
Hello, This is the situation: my script rose.hs is given by data Rose a = a :> [Rose a] root (a :> rs) = a children (a :> rs) = rs and this is what happens when I load it and apply the children function on a rose tree $ ghci GHCi, version 7.8.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :load rose.hs [1 of 1] Compiling Main ( rose.hs, interpreted ) Ok, modules loaded: Main. *Main> children (1 :> [2 :> [], 3 :> []]) <interactive>:3:1: No instance for (Show (Rose t0)) arising from a use of ‘print’ In a stmt of an interactive GHCi command: print it I do not really understand what goes wrong and any suggestion is appreciated. Cheers Lorenzo

It seems to me like there might be benefit to special casing the error
message when (as in this case) the only thing wrong with an expression
is that the resulting type is missing the Show instance implicitly
demanded by ghci.
On Sun, Dec 20, 2015 at 4:06 AM, Lorenzo Isella
On Thu, Dec 17, 2015 at 12:10:01PM +0100, Henk-Jan van Tuyl wrote:
On Thu, 17 Dec 2015 11:37:24 +0100, Lorenzo Isella
wrote: : data Rose a = a :> [Rose a] deriving (Eq, Show)
and the root can be detected simply as
root (a :> rs) = a
I would like to have the expression (with the ":>" notation for the Node) of the function to find the children of the root. Example of expected behavior of this function children
children (1 :> [2 :> [], 3 :> []]) = [2 :> [], 3 :> []]
On top of that, I am trying to get the functions to have the functions
size :: Rose a -> Int leaves :: Rose a -> Int
that count the number of nodes in a rose tree, respectively the number of leaves (nodes without any children). For the children function, I have tried stuff like
children (a :> rs) = rs
quite unsuccessfully.
:
Your definition of children is correct, what is the message you get from the compiler/interpreter?
Regards, Henk-Jan van Tuyl
Hello, This is the situation: my script rose.hs is given by
data Rose a = a :> [Rose a]
root (a :> rs) = a
children (a :> rs) = rs
and this is what happens when I load it and apply the children function on a rose tree
$ ghci GHCi, version 7.8.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> :load rose.hs [1 of 1] Compiling Main ( rose.hs, interpreted ) Ok, modules loaded: Main. *Main> children (1 :> [2 :> [], 3 :> []])
<interactive>:3:1: No instance for (Show (Rose t0)) arising from a use of ‘print’ In a stmt of an interactive GHCi command: print it
I do not really understand what goes wrong and any suggestion is appreciated. Cheers
Lorenzo
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Dear All, This is where I stand now. Please consider the short snippet data Rose a = a :> [Rose a] deriving (Eq, Show) root (a :> rs) = a children (a :> rs) = rs size :: Rose a -> Int size (a :> rs) = 1 + (length $ children (a :> rs)) which defines a rose tree structure and the functions to get the root, the children and the size (i.e. number of nodes) of the rose tree. I would like to find the number of leaves (i.e. terminal nodes) in my rose tree. Essentially, I need to count the number of "[]" inside my rose tree definition. For instance, consider mytree = (1 :> [2 :> [], 3 :> []]) which has exactly two leaves. Can anyone help me implement a function to get the number of leaves? Many thanks Lorenzo

I think that your definition of size is not what you say it is. I guess that you expect (size (1 :> [2 :> [ 3 :> [] ] ])) to be 3. To count leaves you may start with something like: countleaves (_ :> []) = 1 You have then to define countleaves for the case it's not a leaf. I hope this is of some help. On Sun, 2015-12-20 at 20:59 +0100, Lorenzo Isella wrote:
Dear All, This is where I stand now. Please consider the short snippet
data Rose a = a :> [Rose a] deriving (Eq, Show)
root (a :> rs) = a
children (a :> rs) = rs
size :: Rose a -> Int
size (a :> rs) = 1 + (length $ children (a :> rs))
which defines a rose tree structure and the functions to get the root, the children and the size (i.e. number of nodes) of the rose tree. I would like to find the number of leaves (i.e. terminal nodes) in my rose tree. Essentially, I need to count the number of "[]" inside my rose tree definition. For instance, consider
mytree = (1 :> [2 :> [], 3 :> []])
which has exactly two leaves. Can anyone help me implement a function to get the number of leaves? Many thanks
Lorenzo _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (5)
-
David Thomas
-
Henk-Jan van Tuyl
-
Imants Cekusins
-
jean verdier
-
Lorenzo Isella