I have a function that takes in some data and returns an array of values. I need to map a list of data onto a tree.
As I create the tree each call to my function requires three parameters:
- A character from the string
- The index of the character from the string
- Part of the output of the function for the node’s parent (or zero for the root)
For the sake of argument let’s say I have:
input = "ABC"
func :: (Char, Int, Int) -> [(Char, Int, Int)]
func ('A', 1, 0) = [('Q', 1, 1243)]
func ('B', 2, 1243) = [('D', 2, 7512), ('R', 2, 8253)] -- 1243 taken from above
func ('C', 3, 7512) = [('E', 3, 2765)]
func ('C', 3, 8253) = [('Z', 3, 9836)]
Which would map to a tree like:
('Q', 1243)
/ \
('D',7512) ('R',8253)
| |
('E',2765) ('Z',9836)
The first two parameters are fine, I can get those before building the list:
input `zip` [1..]
I’m not sure how to go about getting the third parameter though, since I only know the value for the root node (which will be zero) before I start building the tree. Am I going to have to learn about Monads?
Note: I’m completely new to Haskell and to Functional Programming in general.
Is something like this what you’re after?
Then with your example you’d call it like
buildForest "ABC" func.(Code untested, but if it’s wrong you should still be able to use the general approach.)