Think about two problems:
- How to write a macro to decide whether two strings are equal?
- How to write a
if
macro to do the conditional execution? More specifically, write a macroif(c,x,y)
, it returnsx
ifc
is true, andy
ifc
is false (assume thatc
can either be true or false).
First, we define true and false by:
true() {"T"}
false() {"F"}
They are represented by string literals.
To directly implement the eq
macro is quite hard.
We have to first implement the encode
and decode
macro to help us.
Encoding means to encode a string into another string that has some special properties that we can use.
We define it by this:
We can prove the following results:
- $|x|=1 \land x \neq \mathtt{"\backslash"} \Rightarrow $
$x\mathtt{"^{,}"} \not\subset \mathtt{["\backslash \backslash"/"\backslash"]["\backslash $ "/"$"]["\backslash ^{,}"/"^{,}"]}s$, $x\mathtt{"$"} \not\subset \mathtt{["\backslash \backslash"/"\backslash"]["\backslash $"/"$"]["\backslash ^{,}"/"^{,}"]}s$
encode(s) {
var rep = {
"\" = "\\";
"$" = "\$";
"^" = "\^";
s
};
"_^"+ rep +"_$"
}
decode(s) {
var rep = {
"_^" = "";
"_$" = "";
s
};
"\$" = "$";
"\^" = "^";
"\\" = "\";
rep
}