Understanding the basic principles of Haskell (Monads and so forth), but having not used it for 2 years, I have been struggling for two hours to do this little exercise cleanly:
I want to convert a line of 3*n integers (as in "1 1 1 2 2 2 3 3 3") to a list of 3-tuples of Int (as in [(1,1,1),(2,2,2),(3,3,3)].
This should be done in a straight-forward, error-catching way.
The best solution I could come up with so far contains the following:
groupsOf3 :: [a] -> Maybe [(a,a,a)]
groupsOf3 list =
let fun l = case l of
[] -> []
(x:y:z:rest) -> (Just (x,y,z)) : (fun rest)
_ -> [Nothing]
in sequence $ fun list
That does not seem elegant to me. How would I code this function (with the same interface) more to-the-point?