summaryrefslogtreecommitdiff
path: root/info/challenges/aro-bend/answer.001
blob: 37fe6ace2c086eca9e30485d7fad2c54d97e0514 (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
[Solutions for exercises 1,2,3 were originally posted together on 25 Oct 91]
Date: Fri 25 Oct 91 15:19:44-EST
From: Michael Downes <MJD@MATH.AMS.COM>
Subject: `Around the bend' #1 solutions
To: info-tex@shsu.edu

Solutions to the exercises of `Around the bend' #1.

"*** Exercise 1 (hard):
"Given arbitrary \b, \c, \d (macros without arguments), for example
"
"       \def\b{\c\c}         \def\c{*}            \def\d{\b\c}
"
"figure out how to define \a so that its replacement text consists
"of \b fully expanded plus \c not expanded plus \d expanded exactly once.
"I.e., with the above definitions the replacement text of \a
"should be
"
"       **\c\b\c
"
"You may not use \the or \noexpand in your solution. This is Exercise
"20.16 in the TeXbook, except that there's an added restriction: your
"answer must also not use the \halign ... \span method given in the
"answer to 20.16. (Yes, that means you can't use \valign either!)

The restrictions leave us with (essentially) three expansion-control
commands: \expandafter, \edef and \def.

>>Solution 1 [Peter Schmitt]
   \edef\B{\b}
   \def\defA#1{\def\defa##1##2{\def\a{#1##2##1}}}
   \expandafter\defA\expandafter{\B}
   \expandafter\defa\expandafter{\d}{\c}

>>EndSolution

>>Solution 2 [Donald Arseneau]:
\edef\e{\b}
\expandafter \expandafter \expandafter \def\expandafter \expandafter
\expandafter \a\expandafter \expandafter \expandafter {\expandafter
\e\expandafter \c\d}

>>EndSolution

>>Solution 3 [mine]:
\edef\a{\b}
\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\a
\expandafter\expandafter\expandafter{\expandafter\a\expandafter\c\d}

>>EndSolution

My solution differed from Arseneau's only in using \a rather than \e
in the first step.

[Solution for exercise 2 moved to answer.002]
[Solution for exercise 3 moved to answer.003]

Michael Downes                mjd@math.ams.com (Internet)