import Data.Set (Set, (\\)) import Data.Set qualified as S closure :: Ord a => (a -> a) -> Set a -> Set a closure f s = go S.empty s where go old new | null new = old | otherwise = go old' new' where old' = old <> new new' = S.map f new \\ old' main :: IO () main = print $ closure (`div` 2) (S.singleton 512)