{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreia6mxlylpaxxb4zf653pywvsz24qfy7h5klpax4wbcbmgaqtq5cme",
"uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mm7gndcvu3e2"
},
"path": "/t/lazily-consuming-a-self-referential-linked-list/14131#post_3",
"publishedAt": "2026-05-19T08:42:54.000Z",
"site": "https://discourse.haskell.org",
"textContent": "Applying your suggestions in the quote produces this code:\n\n\n go :: [q] -> s -> (q -> s -> [q] -> ([q], s)) -> ([q], s)\n go qs s0 f = case qs of\n [] -> ([], s0)\n q:rest -> let\n (qs1, s1) = f q s0 rest -- exchanged qs2 for rest\n (qs2, s2) = go qs1 s1 f -- exchanged rest for qs1\n in (qs2, s2) -- exchanged qs1 for qs2\n\n\nI’m not quite sure what kind of behaviour this would be, I feel like the `run` would need to be adjusted as well. Maybe what you’re aiming for is this?\n\n\n go :: [q] -> s -> (q -> s -> [q] -> ([q], s)) -> s\n go qs s0 f = case qs of\n [] -> ([], s0)\n q:rest -> let\n (qs1, s1) = f q s0 rest\n in go qs1 s1\n\n\nWhich would produce a LIFO queue, unless concatenation is used.\n\nI’m afraid the condensed go function might perform poorly when a lot of tasks enqueue two more tasks, leading to catastrophic concatenation. I might just try it… premature optimization is the root of all evil, after all.",
"title": "Lazily consuming a self-referential linked list"
}