summaryrefslogtreecommitdiff
path: root/info/challenges/aro-bend/answer.005
blob: 6c82a057af7241915c983848f41b69016a509cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[Posted to info-tex on 5 Dec 91; see answer.004]
"***********************************************************************
"*** Exercise 5 (fast):
"
"Assuming a normal value for \escapechar,
"
"     \string\a
"
"produces two character tokens. What is the category code of the second?
"Write an experiment (as short as possible) to demonstrate the
"correctness of your answer.

The category of the 'a' token is 12. All tokens produced by \string
have category 12, except for space tokens, which have category 10.

>>Solution 1 [mine]:
\def\answercheck#1#2{\message{#2: \ifcat0#2\else NOT \fi Category 12}}
\expandafter\answercheck\string\a
\answercheck bb

This produces on screen the following message:

a: Category 12 b: NOT Category 12

>>EndSolution

>>Solution 2 [Peter Schmitt]:

\def\test#1#2#3{%
  \message{\ifcat#2#3 #2 and #3 have the same category code
                \else #2 and #3 have not the same category code
           \fi}}

\def\Test#1#2#3{%
  \ifcat#2#3 \message{#2 and #3 have the same category code}
       \else \message{#2 and #3 have not the same category code}
  \fi}

\catcode`\A12
\test 1aA
\Test 1aA
\expandafter\test\string\a A
\expandafter\Test\string\a A

+++++++++++++++++++++++++++++++++++
Comment:
I have given two essentially equivalent Tests --- \test and \Test.

(i) \test is slightly more simple because it contains only one \message
command, but I think that \Test is more adequate because it avoids to
perform the test inside the \message --- there might be some side
effect one is not aware off.

(ii) Both tests are not as short as possible --- the \true and \false
cases could be much shorter, e.g. a T (for true) and a F (for false)
would suffice --- the result could be checked in the dvi-file. (I
regard this difference as inessential.)

Furthermore, setting the catcode of the model character to 12 could
easily be omitted (use some character that is known to be an `other
character'), but I think it should be included: It makes the test
independent of any assumption on the format running. This makes the
solution more closed and selfsufficient, and therefore also simpler and
more elegant (if I may say so).

>>EndSolution