Today I came across these three commands:
\@nil
\@cdr
\@car
Some are mentioned here and here, but I didn't get the idea of it. It seems to have to do with the macro character.
I haven't seen any description about it on google and TeX.SX.
Today I came across these three commands:
\@nil
\@cdr
\@car
Some are mentioned here and here, but I didn't get the idea of it. It seems to have to do with the macro character.
I haven't seen any description about it on google and TeX.SX.
the names come from lisp.
In lisp, car returns the head of a list, cdr returns the tail of a list and nil is an empty list.
in latex
\def\@car#1#2\@nil{#1}
\def\@cdr#1#2\@nil{#2}
(\@nil is not defined at all)
so
\@car abc\@nil
expands to a
and
\@cdr abc\@nil
expands to bc
\@nil is undefined. Why is there no undefined error?
– MaestroGlanz
Feb 13 '17 at 22:02
\def\foo#1\wibblethingy{hello #1}...\foo Maestro\wibblethingy
– David Carlisle
Feb 13 '17 at 22:06
\tl_car:n and tl_cdr:n to honour the cultural heritage
– David Carlisle
Feb 13 '17 at 22:08
\@carand \@cdr the first argument is an undelimited argument and thus leading space tokens might get discarded silently and that pairs of braces surrounding the entire first argument and/or the entire second argument get silently removed. Not always the desired behaviour.
– Ulrich Diez
Feb 14 '17 at 09:27
\@cdr abc de\@nil, the result is bc de. There isn't any space eaten up. It is exactly, what I expect.
– MaestroGlanz
Feb 14 '17 at 09:31
\expandafter\@cdr\@firstofone{␣}abcde\@nil and you will get bcdeas the space token delivered by \@firstofone will not be taken into account when \@cdr "fetches" its first argument. Discarding leading space tokens is standard behaviour when TeX "fetches" undelimited arguments. But it is not standard behaviour when TeX fetches delimited arguments. Therefore \expandafter\@cdr\@firstofone{␣}a␣bcde\@nil will deliver ␣bcde instead of a␣bcde and \expandafter\@car\@firstofone{␣}a␣bcde\@nil will deliver a instead of ␣.
– Ulrich Diez
Feb 14 '17 at 09:49