{-# LANGUAGE LambdaCase #-} data ArbitrarilyNested a = One a | Several [ArbitrarilyNested a] flatten :: ArbitrarilyNested a -> [a] flatten = \case One x -> pure x Several xs -> xs >>= flatten x :: ArbitrarilyNested Integer x = Several [One 1, One 2, Several [One 3, One 4, One 5, One 6], One 7] main :: IO () main = print (flatten x)