Re: ntg-context digest, Vol 1 #715 - 2 msgs
On 7 Apr 2004, at 11:00, ntg-context-request@ntg.nl wrote:
In the following code :
vardef list = A , B , C , A enddef;
def triangle = forsuffixes $=list: pair T.$; endfor; enddef;
beginfig(1) triangle;
path T;
T.A = (2,3); T.B = (3,5); T.C = (2,6);
T = T.A -- T.B -- T.C -- cycle; draw T;
endfig;
end;
In macro "list" if i give just "def" instead of "vardef" the program works fine. But with "vardef" i'm getting many errors. I'm not able to figure this out. Please help;
thanx in advance. Vinuth.
Vinuth, I've been trying to get clear about issues like this recently. Perhaps my answer will not be completely correct. But here goes. One difference between 'def' and 'vardef' is that 'def' makes the symbolic token which follows into a spark, whereas vardef leaves it as a tag. (This is the language that Knuth uses in his Metafont Book.) The difference between sparks and tags is the readiness with which Metafont interprets their meaning. So far as Metafont is concerned, your input consists of a stream of tokens, and at some point in its digestion of your input file it has to interpret each of these tokens. But the stage of digestion at which it interprets a symbolic token depends on whether it is a spark or a tag. It interprets sparks sooner than it interprets tags. Generally speaking - there are ways to prevent it - Metafont interprets sparks in its mouth, whereas the stage at which it interprets tags depends on the circumstances. This is why your program works if you define the symbolic token 'list' using 'def': 'list' becomes a spark whose meaning is 'A,B,C,A'. As soon as Metafont reads 'for suffixes $=list' it interprets 'list', replacing it with 'A,B,C,A'. So writing 'forsuffixes $=list' is equivalent to writing 'forsuffixes $=A,B,C,A'. Perhaps you have guessed now why your program does not work if you define 'list' using 'vardef': 'list' remains a tag and so when Metafont reads 'forsuffixes $=list' it does not interpret it at once. In fact, it never interprets it. For the tags in the suffixes of variable names are never interpreted, and 'forsuffixes' causes 'list' to be treated as the suffix of a variable name. The tags in the suffixes of variable names just form part of the name. Only the subscripts in the suffixes of variable names are interpreted. 'forsuffixes $=list: pair T.$[]; endfor;' is equivalent to 'pair T.list;' Look at the following program: 1 vardef list = 2 1 3 enddef; 4 5 forsuffixes $=list: 6 T $ = 2; 7 endfor; 8 9 show(T list); 10 show(T 1); 11 show(T [list]); 12 13 end; Lines 1-3 assign the meaning the number 1 to the variable name 'list'; they are equivalent to 'list := 1;'. Lines 5-7 make the number 2 the meaning of the variable name 'T list'; they are equivalent to 'T list = 2;'. Line 9 produces the output '>>2', because 2 is the value of 'T list'. Line 10 produces the output '>>T1' because the variable has no value; 'T1' is undefined. Line 11 is interesting. The square brackets around 'list' force evaluation of the variable 'list', so 'show(T [list]);' is equivalent to 'show(T 1);'. The output is '>>T1' again. What do you think that the output of the following program will be? def list = 1 enddef; forsuffixes $=list: T $ = 2; endfor; show(T list); show(T 1); show(T [list]); end; In summary, vardef is intended for giving meanings to variable names. You should be suspicious of 'vardef list = A,B,C,A; enddef;' because A,B,C,A cannot be the value of a variable name. (I assume that this was Hans's point, when he said that Metapost lacks the concept of a list.) If you want to understand things like this, I strongly recommend that you read Knuth's Metafont Book. It makes everything very clear, and nowhere else is the detail explained. The most relevant chapters are the ones on variables, macros, and loops. Best wishes, Jonathan Afterthought: to grasp the difference between a name and the meaning of the name compare '"Vinuth" contains six letters' with 'Vinuth contains six letters.'
participants (1)
-
Jonathan Nicholl