Árbol binario de Haskell con solo caracteres
Frecuentes
Visto 593 equipos
0
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 ')' ?
Edit:
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)
where
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.
1 Respuestas
2
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 haskell tree char binary-tree type-variables or haz tu propia pregunta.
Include the code you tried that didn't work. - Daniel Wagner
Ok I will edit it in 2 mins. - user3129475
templates
etiqueta junto conhaskell
could lead to misunderstanding. Replacing it withtype-variables
- OdomontoisNo incluyes la definición de
expandTree
, so it's not possible to be sure, but I notice that you sometimes callexpandTree
con un argumento deblank
(which is a caller-specified type) and sometimes call it with an argument offront
(el cual es unChar
). These are not obviously the same type, and is probably the source of GHC's complaint. - Daniel WagnerYes 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. - user3129475