When I compile the following Metapost file:
beginfig(1);
def f(expr n) =
if n=0: 0
else: 1
fi;
enddef;
show f(0)+1;
endfig;
end
I expect to get the output 1 (since f(0) is equal to 0, f(0)+1 should be 1!). However, Metapost complains about an Isolated expression.
When I put the expression in parentheses: show (f(0)+1), things get even stranger. The error message becomes : Missing ')' has been inserted. (The first quote should be a backquote, but I couldn't figure out how to escape it). Where on earth was there a mismatched parenthesis??
Thanks for your help!
The
defcommand just expands a name into its definition, so you get literally:The semicolon in the middle is what's wrong, so let us remove it:
This produces the correct expansion:
And outputs
1as expected.On a side note, I'd recommend using begingroup...endgroup for heavier macro definitions, and at least parentheses for lighter ones: for example,
gives
1 + 2 * 2, which is not the same as what is probably expected:which gives
(1 + 2) * 2.