I have a non-binary tree:
data MultTree b = DataNode b | IndexNode Int Int [MultTree b] deriving (Show)
Note: DataNode's are treated like leafs and IndexNode's like branches.
Now I try to achieve that in IndexNode the smaller Int value is set to the smallest value of DataNode of the subtree and the bigger Int value is set to the biggest value of DataNode of the subtree.
For IndexNode's without a subtree the smaller Int value is set to minBound::Int and the bigger one to maxBound::Int
This is my function so far:
dataInterval:: (Ord a) => MultTree a -> MultTree a
dataInterval (DataNode x) = (DataNode x)
dataInterval(IndexNode x y [])
| x > y = (IndexNode (maxBound :: Int) (minBound :: Int) [])
| x < y = (IndexNode (minBound :: Int) (maxBound :: Int) [])
| x == y = (IndexNode x y [])
datenInterval (IndexNode x y subtrees)
| x > y = (IndexNode (maxValue subtrees) (minValue subtrees) (map (dataInterval subtrees)))
| x < y = (IndexNode (minValue subtrees) (maxValue subtrees) (map (dataInterval subtrees)))
| x == y = (IndexNode x y (map (dataInterval subtrees)))
have to call the function dataInterval recursively.
Now I do not know how to do that, because dataInterval expects a tree but somehow I have to call the complete list. dataInterval does not allow that.
Question: How can I achieve calling dataInterval recursively by using the subtrees in the list? So each tree of subtrees list will be called ?
I think it might be some functionality like map, but returning subtrees instead of a list.
At the moment the error message looks like that:
Couldn't match expected type MultTree a2 with actual type [MultTree a] * In the first argument of datenIntervalle, namely subtrees In the first argument of map, namely (datenIntervalle subtrees) In the third argument of IndexNode, namely (map (datenIntervalle subtrees))
sample tree & complete code:
t2 :: MultTree Int
t2 = IndexNode 3 42 [IndexNode 7 8 [DataNode 3, DataNode 5, DataNode 7, DataNode 9], DataNode 6, IndexNode 10 23 [DataNode 99, DataNode 78, DataNode 24]]
dataList:: MultTree a -> [a]
dataList(DataNode x) = [x]
dataList(IndexNode _ _ subtress) = concat' (map dataList subtress)
maxValue :: (Ord a) => MultTree a -> a
maxValue tree = maximum (dataList tree)
minValue :: (Ord a) => MultTree a -> a
minValue tree = minimum (dataList tree)
dataInterval:: (Ord a) => MultTree a -> MultTree a
dataInterval(DataNode x) = (DataNode x)
dataInterval(IndexNode x y [])
| x > y = (IndexNode (maxBound :: Int) (minBound :: Int) [])
| x < y = (IndexNode (minBound :: Int) (maxBound :: Int) [])
| x == y = (IndexNode x y [])
dataInterval(IndexNode x y subtrees)
| x > y = (IndexNode (maxValue subtrees) (minValue subtrees) (map (dataInterval subtrees)))
| x < y = (IndexNode (minValue subtrees) (maxValue subtrees) (map (dataInterval subtrees)))
| x == y = (IndexNode x y (map (dataInterval subtrees)))
dataIntervalsubtreesminValueandmaxValueare not defined, afaik? - Willem Van Onsemmap (dataInterval subtrees)you wantmap dataInterval subtrees- this is actually just what the type error is telling you - besides that it seems like your code compiles just fine. - user2407038