Combining functions in Haskell
How can I combine these similar function in Haskell?
getGroup [] acc = reverse acc
getGroup ((Letter x):letfs) acc = getGroup letfs ((Letter x):acc)
getGroup ((Group x):letfs) acc = getGroup letfs ((Group (makeList x)):acc)
getGroup ((Star x):letfs) acc = getGroup letfs ((Star (makeList x)):acc)
getGroup ((Plus x):letfs) acc = getGroup letfs ((Plus (makeList x)):acc)
getGroup ((Ques x):letfs) acc = getGroup letfs ((Ques (makeList x)):acc)
Letter, Group, Star, Plus and Ques are all part of a data type definition.
data MyData a
= Operand a
| Letter a
| Star [RegEx a]
| Plus [RegEx a]
| Ques [RegEx a]
| Pipe [RegEx a] [RegEx a]
| Group [RegEx a]
deriving Show
I wonder if there is a better way to write those function because of their
similarities. Mostly, I wish to combine the functions for Group, Star,
Plus and Ques, because they are identical, but if there is a method to
combine all of them it would be better.
No comments:
Post a Comment