{"slug": "how-do-i-implement-this-transformation-using-generics", "title": "How do I implement this transformation using generics?", "summary": "The article describes a Haskell data type for integer lists that includes two constructors, `Cons1` and `Cons2`, where `Cons2` embeds a `DeeperList` (which itself wraps a `List`). The author wants to implement a function `incr` that increments each integer by a given value `k` plus the number of nested `DeeperList` levels surrounding that integer, and asks how to implement this using generic programming techniques from the `Data.Data` module, such as `gfoldl` or `gmapT`.", "body_md": "Let’s say I have a data type for integer lists. There are two Cons\nes: one that takes a normal List\n, and one that takes a DeeperList\n, which just embeds a List\n.\ndata DeeperList = DeeperList List\ndata List = Nil\n| Cons1 Int List\n| Cons2 Int DeeperList\nI’d like to write a function incr\n:\nincr :: Int -> List -> List\nsuch that incr k l\nincrements each integer in l\nby k\nplus the number of DeeperList\ns that embeds that integer. For example:\nincr 3 $ Cons1 2 $ Cons2 4 $ DeeperList $ Cons2 0 $ DeeperList $ Cons1 5 Nil\n-------------------------------------------------------------------\n-- represented more compactly as [ 2, 4, D[ 0, D[ 5 ]]]\nshould be:\nCons1 5 $ Cons2 7 $ DeeperList $ Cons2 4 $ DeeperList $ Cons1 10 Nil\n-------------------------------------------------------------------\n-- [ 5, 7, D[ 4, D[ 10 ]]]\nI’d like to implement incr\nwith generics, specifically with something like gfoldl\n, gmapT\n, etc., stuff from Data.Data\n. How do I do that?", "url": "https://wpnews.pro/news/how-do-i-implement-this-transformation-using-generics", "canonical_source": "https://discourse.haskell.org/t/how-do-i-implement-this-transformation-using-generics/14153#post_1", "published_at": "2026-05-23 20:54:40+00:00", "updated_at": "2026-05-23 21:05:10.177418+00:00", "lang": "en", "topics": [], "entities": [], "alternates": {"html": "https://wpnews.pro/news/how-do-i-implement-this-transformation-using-generics", "markdown": "https://wpnews.pro/news/how-do-i-implement-this-transformation-using-generics.md", "text": "https://wpnews.pro/news/how-do-i-implement-this-transformation-using-generics.txt", "jsonld": "https://wpnews.pro/news/how-do-i-implement-this-transformation-using-generics.jsonld"}}