import Data.List (find) data Family = Family { person :: String, mother :: String, father :: String } deriving (Show, Eq) relatedFamilyData :: [Family] relatedFamilyData = [ Family {person = "Alice", mother = "Mary", father = "John"}, -- Alice is the child of Mary and John Family {person = "Mary", mother = "Grace", father = "Robert"}, -- Mary is the child of Grace and Robert Family {person = "John", mother = "Emma", father = "Michael"}, -- John is the child of Emma and Michael Family {person = "Grace", mother = "Anna", father = "David"}, -- Grace is the child of Anna and David Family {person = "Robert", mother = "Eve", father = "James"} -- Robert is the child of Eve and James ] findFamilyByName :: String -> Maybe Family findFamilyByName name = find (\record -> person record == name) relatedFamilyData extendedFamily :: Maybe Family -> String -> [String] extendedFamily (Just family) to | not (null motherAncessors) = motherAncessors | not (null fatherAncessors) = fatherAncessors | otherwise = [] where motherAncessors = ancessors (mother family) to fatherAncessors = ancessors (father family) to extendedFamily Nothing _ = [] ancessors :: String -> String -> [String] ancessors from to = if from == to then [to] else from : extendedFamily (findFamilyByName from) to -- Print the list of family records main :: IO () main = do mapM_ print (ancessors "Alice" "Eve")