cd /news/general/how-do-i-implement-this-transformati… · home topics general article
[ARTICLE · art-12416] src=discourse.haskell.org ↗ pub= topic=general verified=true sentiment=· neutral

How do I implement this transformation using generics?

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`.

read1 min views22 publishedMay 23, 2026

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 . data DeeperList = DeeperList List data List = Nil | Cons1 Int List | Cons2 Int DeeperList I’d like to write a function incr : incr :: Int -> List -> List such that incr k l increments each integer in l by k plus the number of DeeperList s that embeds that integer. For example: incr 3 $ Cons1 2 $ Cons2 4 $ DeeperList $ Cons2 0 $ DeeperList $ Cons1 5 Nil

-------------------------------------------------------------------
-- represented more compactly as [ 2, 4, D[ 0, D[ 5 ]]]

should be: Cons1 5 $ Cons2 7 $ DeeperList $ Cons2 4 $ DeeperList $ Cons1 10 Nil

-------------------------------------------------------------------
-- [ 5, 7, D[ 4, D[ 10 ]]]

I’d like to implement incr with generics, specifically with something like gfoldl , gmapT , etc., stuff from Data.Data . How do I do that?

sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/how-do-i-implement-t…] indexed:0 read:1min 2026-05-23 ·