Back to Scan

Iterators · Scan · Practice Question

Step Through a Backward-Fill Using Scan

Sign in to track your progress

Question

Given:

l:10 8 0n 1 0n 0n 12 13 0n 7

Use scan to see every intermediate step of replacing each null with the next non-null value.

Solution

{(next x)^x}\[l]

Expected result (one row per step):

10 8   1       12 13   7
10 8 1 1    12 12 13 7 7
10 8 1 1 12 12 12 13 7 7

Explanation

(next x)^x fills every null in x with the value immediately after it (^ fills the nulls in its right argument with the corresponding values from its left argument). On a list with consecutive nulls, one pass isn't enough — a null followed by another null just gets filled with another null.

Calling this with Converge scan (\, no iteration count given) reruns the function on its own output until the result stops changing, and — because it's scan rather than over — keeps every pass along the way:

  • Row 1 is the original list — Converge Scan always starts with its argument.
  • Row 2 is the result of pass 1, which fills every null that has a non-null value immediately after it.
  • Row 3 is the result of pass 2, which fills the nulls that are now next to a freshly-filled value.
  • Pass 3 produces the same list again, so Converge stops without adding another row.

This is the same Converge mechanic documented on the Iterators overview page for raze/ — here applied with Scan instead of Over, specifically to see the layer-by-layer progress rather than jumping straight to the final answer. Compare with the companion Over question, which calls the exact same function but returns only that last row.