Skip to content

Instantly share code, notes, and snippets.

@abuseofnotation
Last active March 18, 2025 09:34
Show Gist options
  • Save abuseofnotation/b6614820b215ae95481507aaa4e90d48 to your computer and use it in GitHub Desktop.
Save abuseofnotation/b6614820b215ae95481507aaa4e90d48 to your computer and use it in GitHub Desktop.
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
| from == to = [to]
| not (null family) = from : family
| otherwise = family
where
family = extendedFamily (findFamilyByName from) to
-- Print the list of family records
main :: IO ()
main = do
mapM_ print (ancessors "Alice" "Emma")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment