[metapost] short-circuit logic
Laurent Méhats
laurent.mehats at gmail.com
Thu Jun 23 12:33:26 CEST 2011
Le 23/06/2011 00:33, Troy Henderson a écrit :
> Does MetaPost have a short-circuit logical "or" and "and"?
>
> That is, if a and b are booleans, and if a is false, then "a and b"
> should evaluate to false independent of the value of b (and should not
> even consider b).
>
> Similarly, if a is true, then "a or b" should evaluate to true
> independent of the value of b (and should not even consider b).
>
> Troy
> --
> http://tug.org/metapost/
Hello,
Here are attempts that go the way you describe.
vardef lazyand (text CONDS)=
boolean res;
res:=false;
for cond=CONDS:
res:=scantokens cond;
exitif not res;
endfor
res
enddef;
vardef lazyor (text CONDS)=
boolean res;
res:=true;
for cond=CONDS:
res:=scantokens cond;
exitif res;
endfor
res
enddef;
Both 'lazyand' and 'lazyor' expect arbitrary long (possibly empty)
parenthesized lists of conditions in double quotes. As an example,
assuming 'var' is unknown, 'lazyor ("unknown var", "var=0")' returns
'true' while '(unknown var) or (var=0)' fails since metapost tries to
evaluate 'var=0': '! Unknown relation will be considered false.'
Regards,
Laurent Méhats
More information about the metapost
mailing list