Árbol binario de Haskell con solo caracteres

Lets say I make my tree like this

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)

But I want to make a tree that uses only chars I don't need it to be template. I know my tree will use only chars if I make it like the one above I can't make if dataOfNode == ')' in a function because It says that it is a [char] type and it expects [a] type.

How to make a tree that would use only chars or is there a way to make this type of check dataOfNode == ')' with this type of tree that I gave. Can you give me a short example of a function that let's say check if the data in this node is the sign ')' ?

As requested I am posting the function that I would like to make `

buildTreeHelper :: (Ord a) => String -> a -> Int -> String -> Tree a -> Tree a
buildTreeHelper str blank turn path t
 | str == [] = t
 | front == '(' = buildTreeHelper (tail str) blank 1 ('L':path) (expandTree (reverse path) blank t)
 | front == ')' = buildTreeHelper (tail str) blank 2 (tail path) t
 | turn == 1 = buildTreeHelper (tail str) blank 2 (tail path) (expandTree (reverse path) front t)
 | turn == 2 = buildTreeHelper (tail str) blank 3 ('R':path) (expandTree (reverse path) front t)
 | turn == 3 = buildTreeHelper (tail str) blank 2 (tail path) (expandTree (reverse path) front t)
  front = head str

Logically it is not finished but also I can't use it because it takes a string which I divade char by char. That's why I am sure I want to make my Tree only from chars so I can replace type a a type char at the begining of the function. It has other mistakes too but I think the main idea is clear.

preguntado el 05 de febrero de 14 a las 17:02

Include the code you tried that didn't work. -

Ok I will edit it in 2 mins. -

templates etiqueta junto con haskell could lead to misunderstanding. Replacing it with type-variables -

No incluyes la definición de expandTree, so it's not possible to be sure, but I notice that you sometimes call expandTree con un argumento de blank (which is a caller-specified type) and sometimes call it with an argument of front (el cual es un Char). These are not obviously the same type, and is probably the source of GHC's complaint. -

Yes you are right. I made it like this because I wanted to be clearer where the problem is. Blank is also a constant char and It can't be anything else so I changed it too to 'char' type and it works fine. -

1 Respuestas

You easily could replace your type definition with

data Tree = EmptyTree | Node Char Tree Tree deriving (Show, Read, Eq)

or specialise just your function defining it as

buildTreeHelper :: String -> Char -> Int -> String -> Tree Char -> Tree Char

Respondido 05 Feb 14, 17:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.