Lazily consuming a self-referential linked list
Haskell Community [Unofficial]
May 19, 2026
tobz619:
> VegOwOtenks:
>
>>
>> go :: [q] -> s -> (q -> s -> [q] -> ([q], s)) -> ([q], s)
>> go qs s0 f = case qs of
>> [] -> ([], s0)
>> q:rest -> let
>> (qs1, s1) = f q s0 qs2 -- why is this qs2 and not rest?
>> (qs2, s2) = go rest s1 f -- why is this rest and not qs1?
>> in (qs1, s2) -- why is this qs1 and not qs2?
>>
The order is like this because it allows the list that is build-up to travel-backwards along the computation, like it would with TardisT. This enables cheap list-building (only cons, no concatenation) while preserving FIFO semantics.
Discussion in the ATmosphere