As I'm constructing my data structure I found an issue that I'm having trouble solving it. My data structure "Structure" is String and a list of subStructures.
The issue is in the "lookFor" method below, that is supposed to look for a subStructure named "a" in (Structure b xs) and return it. If not on "b"'s list (xs), it should continue looking in each xs element's list and so on. If not found it should do nothing.
My idea was recursion, and for that, I thought "map lookFor xs" in case it hasn't found the Structure named "a".
Ghci says "Couldn't match expected type Structure with actual type [Structure]" Which I understand, because after all map returns a list of elements and not an element.
data Structure = Structure String [Structure]
name :: Structure -> String
name (Structure a xs) = a
subStrcts :: Structure -> [String]
subStrcts (Structure a []) = []
subStrcts (Structure a xs) = [name x | x <- xs]
lookFor :: String -> Structure -> Structure
lookFor a (Structure b xs)
| elem (elemIndex a (subStrcts (Structure b xs))) [0..] = xs !! (fromJust (elemIndex a (subStrcts (Structure b xs))))
| otherwise = map (lookFor a) xs
Any ideas? Thanks
lookForreturn aMaybe Structure?Structurethat matches the query, right? In case you do not find one, you can not simply returnnull(well Haskell has anundefined, but it is usually better not to use that). In that case you can use aMaybe Structure. AMaybe a, has two possible valuesNothing(i.e. the query did not give a result), orJust xwithxthe result.