{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreig6nqgmey5ep4eowp7bsvvji2l6ilgo5hhfjws2euox6iwtlygnxa",
"uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mmkgy2l3yj62"
},
"path": "/t/how-do-i-implement-this-transformation-using-generics/14153#post_1",
"publishedAt": "2026-05-23T20:54:40.000Z",
"site": "https://discourse.haskell.org",
"textContent": "Let’s say I have a data type for integer lists. There are two `Cons`es: one that takes a normal `List`, and one that takes a `DeeperList`, which just embeds a `List`.\n\n\n data DeeperList = DeeperList List\n data List = Nil\n | Cons1 Int List\n | Cons2 Int DeeperList\n\n\nI’d like to write a function `incr`:\n\n\n incr :: Int -> List -> List\n\n\nsuch that `incr k l` increments each integer in `l` by `k` plus the number of `DeeperList`s that embeds that integer. For example:\n\n\n incr 3 $ Cons1 2 $ Cons2 4 $ DeeperList $ Cons2 0 $ DeeperList $ Cons1 5 Nil\n -------------------------------------------------------------------\n -- represented more compactly as [ 2, 4, D[ 0, D[ 5 ]]]\n\n\nshould be:\n\n\n Cons1 5 $ Cons2 7 $ DeeperList $ Cons2 4 $ DeeperList $ Cons1 10 Nil\n -------------------------------------------------------------------\n -- [ 5, 7, D[ 4, D[ 10 ]]]\n\n\nI’d like to implement `incr` with generics, specifically with something like `gfoldl`, `gmapT`, etc., stuff from `Data.Data`. How do I do that?",
"title": "How do I implement this transformation using generics?"
}