For readers who don't find the line:
\def\\{\if\space\next\ % assume that \next is unexpandable
in their TeXbook, it is in the errata published by Donald E. Knuth (“page A311”).
Suppose you do:
\def\foobar{cat}
\noindent
\demobox{The \foobar\ in the hat}
After \next has grabbed the \foobar token, \\ will expand to something equivalent to:
\if\space\next\ %
\else \setbox0=\hbox{\next}\maketypebox\fi
with \next being \let-equal to \foobar. According to the documentation of \if (TeXbook p. 209), TeX is going to expand tokens following the \if until it finds two non-expandable ones. \space expands to an explicit space token in one step, so TeX goes on with \next (which has the same meaning as \foobar at this point), because it needs one more non-expandable token. After \next has been expanded, the input is equivalent to:
\if〈space token〉cat\ %
\else \setbox0=\hbox{\next}\maketypebox\fi
where 〈space token〉 represents an explicit space token (one could define a control sequence that is \let-equal to an explicit space token and use it instead of 〈space token〉, see footnote 1 below). Now, TeX has two non-expandable tokens following the \if: a space token and a c character token (of category 11 under the normal catcode regime). So, the outcome of the \if can be decided: it is false because the character codes of a 〈space token〉 and of c differ, so TeX will skip to the \else clause.
There is no big problem so far, though we are going to box the whole cat at once instead of each character separately (c, a, and t); but let's back up a little bit. Had we used:
\def\foobar{ cat}
the input would have been equivalent to:
\if〈space token〉〈space token〉cat\ %
\else \setbox0=\hbox{\next}\maketypebox\fi
The test would have been true and TeX would have left cat\ in the input stream, which is plain wrong, because we were supposed to test what we just grabbed in \next, not to insert new text!
So, the comment “assume that \next is unexpandable” could be rephrased more generally, in my humble opinion, as “assume that \next ultimately expands to either (1) exactly one character token or (2) exactly one \chardef token or (3) a control sequence token that is \let-equal to (1) or (2)”2 (the character token in (1) is necessarily non-active, because of the “ultimately”). Indeed, you can test that \demobox works perfectly when \next recursively expands to a single character token, as in:
\def\myspacei{\myspace}
\def\myspace{\space}
\noindent
\demobox{Abc def\myspacei pU gHi}

Using \myspacei here gives the same result as using an explicit space token, because it recursively expands to such a token.
Here is another example that additionally uses a control sequence that recursively expands to a non-space character token:
\def\myspacei{\myspace}
\def\myspace{\space}
\def\myxii{\myxi}
\def\myxi{\myx}
\def\myx{X}
\noindent
\demobox{Abc def\myspacei pU\myxii gHi}

Your proposal:
\def\\{\expandafter\ifx\space\next\ %
...
would also work, as long as \next has been \let-equal to a space token (explicit or implicit). But it wouldn't work with input containing spaces in the form of macros like \space or our \myspacei macro defined above. Indeed, \ifx distinguishes between character tokens and macros (see specification of \ifx p. 210 of the TeXbook).
Finally, although it would work, your replacement of \endlist with \end does not sound like the best coding style to me, because \end is an existing TeX primitive; Knuth chose something more “unique” to mark the end of the text to be worked on. Besides, the name \endlist was visibly chosen to match \dolist: it is a matter of consistency. See in particular:
\def\demobox#1{\setbox0=\hbox{\dolist#1\endlist}%
...
Footnotes
You can define a control sequence \stoken that is \let-equal to an explicit space token like this:
{\def\\{\global\let\stoken= }\\ }% now, \stoken is an implicit space token
(adapted from the TeXbook p. 376). Two other ways are given in the TeXbook p. 336 (exercise 24.6):
\def\\{\let\stoken= }\\ %
and
\def\\#1\\{}\futurelet\stoken\\ \\%
This is in particular the case when \next has been \let-equal to a non-active character token—which is, I think, the case Knuth had in mind when he used the word “unexpandable” (indeed, a non-active character token, or a control sequence that has been \let-equal to such a token, never expands). In other words, the condition “\next is unexpandable” from the comment you quoted is a sufficient condition for ensuring that the macros behave sanely, and is only a particular case of the more general condition I gave. :-)
\expandaftermakes\spacea space token. – Igor Liferenko May 27 '19 at 08:46\endis used just as a token here. Knuth uses it a lot. For example, see final example in chapter 20. – Igor Liferenko May 27 '19 at 08:49\spaceis defined in plain.tex with\def\space{ }. Anything defined with\def(or one of its variants) is a macro, that is the definition of a macro. If you still don't believe me, runtexdef space. This outputs\space: macro:->. – frougon May 27 '19 at 08:50\expandafteron p. 213 – Igor Liferenko May 27 '19 at 08:51\endworks... until you do for instance\demobox{Some\end text}. Then it fails. That is why it is better to use something really “unique”. In LaTeX2e,\@nilis often used for this purpose, and LaTeX3 has other control sequence tokens for this kind of thing. – frougon May 27 '19 at 08:53\expandafterp. 213? It doesn't even use the word “macro”! – frougon May 27 '19 at 08:55\if\space\next...and\expandafter\ifx\space\next...produce the same result (at least when\nextis a simple character token). Run TeX and check. – Igor Liferenko May 27 '19 at 08:59\demobox{Some\endlist text}also does not work, for that matter. – Igor Liferenko May 27 '19 at 09:06\expandafter\ifx\space\next, I forgot to “do” the\expandafter. This is corrected in my answer. Your counter-example with\endlistis also correct. My answer contains the reasons that, in my opinion, justify using\endlistrather than\end. – frougon May 27 '19 at 18:07\def\endlist{\endlist}. Not sure why this\defwas added at all (considering the object of the exercise). – Igor Liferenko May 28 '19 at 06:57\endlist, it is not designed for this kind of use”. Since the definition would immediately cause an infinite loop if\endlistever became the next token to process in TeX' s input stream (expanded, expanded again, etc.), it makes it clear that the purpose of this token is different: it is a marker (technically here, an argument delimiter). LaTeX3 quarks are defined in the same way, seel3quarkin interface3.pdf. – frougon May 28 '19 at 07:13\endlist, TeX directly says! Undefined control sequence., so I still don't really understand what this\defis about. Moreover, a comment in the code like% don't execute \endlistwould be more appropriate for such a small exercise. – Igor Liferenko May 28 '19 at 07:25Undefined control sequence, you must execute it when still undefined... If I take the file containing the\demoboxcode from yesterday and append\endlistright after\def\endlist{\endlist}(uncommented!), then running TeX on the file causes an infinite loop, as expected. The cause of the infinite loop can be found by doing\tracingmacros=1\tracingonline=1\relaxearly enough, which shows an endless stream of\endlist ->\endlistexpansions. So, this definition is a debugging aid: when you use it in a wrong way, it tells you if you do the\tracingmacros=1\tracingonline=1. – frougon May 28 '19 at 07:31\cs, how does one determine what it is? I would put\show\csto that part of manuscript where I see\cs. Thus, if\show\csshows "undefined", I already know that it is not to be expanded. As for "ensuring that different control sequences don't have the same meaning", names of control sequences serve just this purpose (i.e., if you name control sequences with different names, they have different meaning). So, this whole concept of "quark" is strange to me. – Igor Liferenko May 28 '19 at 08:07\def\a{}\ifx\a\empty true\else false\fiprints true.\aand\emptyhave different names but the same meaning.\endlist(for whatever reason) as\chardef\endlist=\\X2) this definition is made after or instead of Knuth's one (overrides it) 3) the user has an equivalent definition like\let\a\endlistor\chardef\a=`\X**then**\demobox{Abc \a stuff that won't get boxed}won't work as expected, even though\demobox's argument respects Knuth's (and therefore my) condition regarding\nextfor every token grabbed from this argument, in particular when\nextis\let-equal to\a(\a` is indeed unexpandable). – frougon May 28 '19 at 09:20\endlistdefinition gives a signal that if the user redefines it in some particular ways like shown in my previous comment (too long to put all in one), this may break\demobox. – frougon May 28 '19 at 09:20