import Control.Monad (mapM_) import Data.List (partition) -- | Split off any nums that satisfy the relation and return the split and the rest in a tuple splitOffRelatedNums :: (Integer -> Integer -> Bool) -> [Integer] -> ([Integer],[Integer]) splitOffRelatedNums relation nums = partition (relation x) nums where x = head nums -- | Split off equivalence groups until there are no more that satisfy the condition getEquivalenceClasses :: (Integer -> Integer -> Bool) -> [Integer] -> [[Integer]] getEquivalenceClasses relation nums = case nums of [] -> [] nums -> x : getEquivalenceClasses relation xs where (x,xs) = splitOffRelatedNums relation nums main :: IO () main = do let setA = [1..7] relation :: Integer -> Integer -> Bool relation x y = (x - y) `mod` 3 == 0 mapM_ print $ getEquivalenceClasses relation setA