The most general polymorphic type is
unfold :: (s -> Bool) -> (s -> a) -> (s -> s) -> s -> [a]
The stopping predicate consumes a state, the output function turns that state into one list element, and the transition function supplies another state of the same type. The state type and output type need not coincide. This is a list unfold with separate stopping, output and transition arguments.
The standard Haskell definition and principal type are
iterate :: (a -> a) -> a -> [a]
iterate f x = x : iterate f (f x)
Under lazy evaluation, this produces successive states as demanded. An infinite spine does not require all elements to be evaluated at once; productivity still depends on the demanded computations terminating.
The required function composition is
unfold p f g = map f . takeWhile (not . p) . iterate g
iterate g supplies the successive states, takeWhile (not . p) retains precisely those before the first stopping state, and map f produces their outputs. If the initial state satisfies the stopping predicate, both sides return the empty list. Otherwise both emit the same head and repeat from g x; the same argument applies to every demanded finite prefix of an infinite result.
Using the specified list unfold, the definitions are
map f     = unfold null (f . head) tail
iterate f = unfold (const False) id f
For map, the current state is the remaining input list. null prevents head and tail from being evaluated on an empty list. For iterate, the stop predicate is always false and the output is the current state itself. Its transition is f, so the original seed is emitted before the first application of f.
Store two consecutive Fibonacci numbers as the state:
fibs :: [Integer]
fibs = unfold (const False) fst step (0,1)
  where
    step (a,b) = (b,a+b)
The invariant is that the state is , with . Emitting its first component and updating the pair therefore produces . Integer avoids fixed-width overflow; the bit cost of adding increasingly large integers is separate from the number of unfold steps.
Arrange that each state already begins with the next passing element, or is empty:
filter p = unfold null head next . seek
  where
    seek = dropWhile (not . p)
    next = seek . tail
The initial seek skips the rejected prefix. After an element is emitted, next removes it and skips the following rejected run. Thus null guards both partial list operations and the filtering a list by unfolding invariant is preserved.
For a finite input of length and constant-cost predicate evaluation, each input element is tested once and each list cell is passed a bounded number of times: the total work is , as for the usual recursive list filter. This version may have different constant overhead. Recomputing dropWhile independently in the stopping, output and transition expressions could repeat scans; keeping the already-filtered suffix as state avoids that. On an infinite rejected tail, finding another output diverges, just as the standard lazy filter does.

Articles by others on the same topic (0)

There are currently no matching articles.