Generate a list from a state: a step returning Nothing terminates; Just (x,s') emits x and continues from s'. Thus unfoldr step s = case step s of Nothing -> []; Just (x,s') -> x : unfoldr step s'. A list unfold decomposes the evolving state, whereas a list fold consumes existing constructors. Productivity depends on each demanded constructor becoming available, not merely on using lazy evaluation.
For a predicate , normalize the state to the first remaining passing element with seek = dropWhile (not . p). Stop on an empty state, otherwise emit its head and continue from seek (tail state). The invariant is that every nonempty state starts with a passing element. An initial seek establishes it; removing the emitted head and seeking again preserves it. Each rejected run is traversed once, giving linear time in the examined input prefix under constant-cost predicate tests.

Articles by others on the same topic (0)

There are currently no matching articles.