an advanced foldification problem

I have a data type "Group", representing a group of geographic information that is all referring to the same location, and a function "mergeGroups" that tries to merge two groups: mergeGroups :: Group -> Group -> Maybe Group Then I have a function "mergeGroupToList" that tries to merge its first argument with every element of the second argument: mergeGroupToList :: Group -> [Group] -> [Group] mergeGroupToList g [] = [g] mergeGroupToList g1 (g2:gs) = case (mergeGroups g1 g2) of Nothing -> g2 : (mergeGroupToList g1 gs) Just g3 -> mergeGroupToList g3 gs How can I rewrite mergeGroupToList in terms of foldr?

On Thu, 11 Jan 2007, Seth Gordon wrote:
I have a data type "Group", representing a group of geographic information that is all referring to the same location, and a function "mergeGroups" that tries to merge two groups:
mergeGroups :: Group -> Group -> Maybe Group
Then I have a function "mergeGroupToList" that tries to merge its first argument with every element of the second argument:
mergeGroupToList :: Group -> [Group] -> [Group] mergeGroupToList g [] = [g] mergeGroupToList g1 (g2:gs) = case (mergeGroups g1 g2) of Nothing -> g2 : (mergeGroupToList g1 gs) Just g3 -> mergeGroupToList g3 gs
Is mapAccumL also allowed? Untested but type-checked: mergeGroupToList g0 gs = let (mergedG, unmergedGs) = mapAccumL (\g1 g -> maybe (g1, Just g) (\g3 -> (g3, Nothing)) (mergeGroups g1 g)) g0 gs in catMaybes unmergedGs ++ [mergedG]

Take this obscure function: \begin{code} func :: (a -> a -> Maybe a) -> a -> [a] -> [a] func f s0 xs0 = foldr (\x xs s -> maybe (xs s) ((x:) . xs) (f s x)) return xs0 s0 \end{code} And mergeGroupToList becomes: \begin{code} mergeGroupToList g xs = func mergeGroups g xs \end{code} Cheers, Spencer Janssen On Jan 11, 2007, at 11:37 AM, Seth Gordon wrote:
I have a data type "Group", representing a group of geographic information that is all referring to the same location, and a function "mergeGroups" that tries to merge two groups:
mergeGroups :: Group -> Group -> Maybe Group
Then I have a function "mergeGroupToList" that tries to merge its first argument with every element of the second argument:
mergeGroupToList :: Group -> [Group] -> [Group] mergeGroupToList g [] = [g] mergeGroupToList g1 (g2:gs) = case (mergeGroups g1 g2) of Nothing -> g2 : (mergeGroupToList g1 gs) Just g3 -> mergeGroupToList g3 gs
How can I rewrite mergeGroupToList in terms of foldr? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Henning Thielemann
-
Seth Gordon
-
Spencer Janssen