|
From: Stavros M. <mac...@gm...> - 2024-01-31 00:55:03
|
Looks about right. But you don't need eargs:ev(args) -- that will end up evaluating the argument twice, as you can see here: assert(2>1,print(oops)) => prints oops Also, I'd use *is(...) *instead of *ev(...,pred)*, because *ev* is a weird DWIM <https://en.wikipedia.org/wiki/DWIM>-type command designed for interactive use which does surprising things sometimes. -s PS An example of an *ev* surprise: assume(neg<0,pos>0)$ neg:'pos$ is('neg<'pos) => true ev('neg<'pos,pred) => false test:'(neg<pos) ev(test,pred) => false is(test) => true On Tue, Jan 30, 2024 at 4:28 PM Henry Baker <hb...@pi...> wrote: > I've written plenty of macros in Lisp, but this is my first > > attempt at a Maxima macro, so don't laugh! > > > > assert(<boolean expression>, <stuff to print>...) > > > > Evaluate <boolean expression> and call error if the boolean > > expression evaluates to 'false'. > > > > The rest of the args are passed along to 'error' when it is > > called. > > > > I'm not comfortable enough with Maxima macros or 'buildq' to > > know if I'm using the local variables correctly. > > > > Any improvements to suggest ? > > > > maxima -b assert.mac > > Maxima 5.45.1 https://maxima.sourceforge.io > using Lisp GNU Common Lisp (GCL) GCL 2.6.12 > Distributed under the GNU Public License. See the file COPYING. > Dedicated to the memory of William Schelter. > The function bug_report() provides bug reporting information. > (%i1) batch("assert.mac") > > read and interpret /home/foobar/assert.mac > (%i2) assert(be,[args])::=buildq([sbe:string(be),ebe:ev(be,pred), > eargs:ev(args)], > if not ebe > then apply('error,append(["assert",sbe,"failed"],eargs)) > else true) > (%o2) assert(be, [args]) ::= buildq([sbe : string(be), ebe : ev(be, pred), > eargs : ev(args)], if not ebe then apply('error, > append(["assert", sbe, "failed"], eargs)) else true) > (%i3) foo:3 > (%o3) 3 > (%i4) assert(foo > 2,"foo =",foo) > (%o4) true > (%i5) assert(foo > 3,"foo =",foo) > assert foo > 3 failed foo = 3 > -- an error. To debug this try: debugmode(true); > (%o6) /home/foobar/assert.mac > > > _______________________________________________ > Maxima-discuss mailing list > Max...@li... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > |