Task

Draw a Cantor set.



See details at this Wikipedia webpage:   Cantor set

CANTOR
=LAMBDA(n,
    APPLYN(n)(
        LAMBDA(grid,
            APPENDROWS(grid)(
                CANTOROW(
                    LASTROW(grid)
                )
            )
        )
    )({0,1})
)
 
 
CANTOROW
=LAMBDA(xys,
    LET(
        nCols, COLUMNS(xys),
 
        IF(2 > nCols,
            xys,
            IF(3 < nCols,
                APPENDCOLS(
                    CANTORSLICES(TAKECOLS(2)(xys))
                )(
                    CANTOROW(DROPCOLS(2)(xys))
                ),
                CANTORSLICES(TAKECOLS(2)(xys))
            )
        )
    )
)
 
 
CANTORSLICES
=LAMBDA(ab,
    LET(
        a, INDEX(ab, 1),
        b, INDEX(ab, 2),
        third, (b - a) / 3,
 
        CHOOSE({1,2,3,4}, a, a + third, b - third, b)
    )
)
 
 
SHOWCANTOR
=LAMBDA(grid,
    LET(
        leaves, LASTROW(grid),
        leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1),
        leafCount, 1 / leafWidth,
 
        SHOWCANTROWS(leafCount)(grid)
    )
)
 
 
SHOWCANTROWS
=LAMBDA(leafCount,
    LAMBDA(grid,
        LET(
            xs, FILTERP(
               LAMBDA(x, NOT(ISNA(x)))
            )(
                HEADCOL(grid)
            ),
 
            runLengths, LAMBDA(x, 
                CEILING.MATH(leafCount * x))(
                    SUBTRACT(TAILROW(xs))(INITROW(xs)
                )
            ),
 
            iCols, SEQUENCE(1, COLUMNS(runLengths)),
 
            CONCAT(
                REPT(
                    IF(ISEVEN(iCols), " ", "█"), 
                    runLengths
                )
            ) & IF(1 < ROWS(grid),
                CHAR(10) & SHOWCANTROWS(leafCount)(
                    TAILCOL(grid)
                ),
                ""
            )
        )
    )
)