|
From: Fraser D. <dos...@gm...> - 2020-10-30 20:33:26
|
Will be busy for now. Changing the body of all expressions in structs from strings to expressions. This will require breaking apart large expressions and then using assoc with nested lists. Entering more CMT equations into maxima. Getting the simplest example that works with this math is not that simple.:-) On 2020-10-24 1:24 p.m., Stavros Macrakis wrote: > Function definitions are useful when you want to /transform/ your > input in some way, or apply logic. They can iterate/recurse over > structures easily. They can perform symbolic or numerical > calculations. In other words, for programming. > > Mathematical expressions should almost never be expressed as defined > functions. > > Macros are a rather specialized mechanism inherited from Lisp. They > are functions for writing functions. They are generally used to > capture coding idioms that are important in your work. They are > sometimes used for efficiency when compiling code, as a form of > inlining, but you should rarely run into that situation. The main > advantage of macros over functions is that when you call them, they > don't evaluate their arguments, so you can use them for control > structures. For example, if Maxima didn't support the > *while* statement, you could write it as a macro: > > mywhile(condition,body) ::= buildq([condition,body], do (if > condition then body else return() ))$ > > and then invoke it: > > x:0$ > mywhile(x<3,(print(x),x:x+1)))$ > 0 > 1 > 2 > > You can see how this works by using *macroexpand*: > > macroexpand(mywhile(x<3,(print(x),x:x+1))) > => do if x < 3 then (print(x), x : x + 1) else return() > > *buildq* is a convenience function equivalent to subst in many cases > (though it also adds *splice*). I don't recommend using it except for > defining macros, but that's a question of style. In this case, you > could write equivalently: > > mywhile(condition,body) ::= > subst(['condition=condition,'body=body], '(do (if condition then body > else return() )))$ > > You could, of course, also rewrite *while*//as a /function /(not a > macro) in pseudo-functional style: > > whilef(condition,body):=do (if condition() then body() else return())$ > whilef(lambda([],x<3),lambda([],print(x),x:x+1)) > > I call it pseudo-functional because it depends on a side-effect, > namely assignment. > Keep in mind that Maxima has dynamic scope, so the lambda expressions > shouldn't use the variables *condition*//and *body*. Macros don't have > that problem. > > -s > > On Sat, Oct 24, 2020 at 6:11 AM Fraser Doswell <dos...@gm... > <mailto:dos...@gm...>> wrote: > > If expressions are built up in parts from smaller expressions using > subst, then functions (:= and define) are better for ancillary or > static > code. But where do macros, ::=, buildq work best? > > Fraser > > > > _______________________________________________ > Maxima-discuss mailing list > Max...@li... > <mailto:Max...@li...> > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > > > > _______________________________________________ > Maxima-discuss mailing list > Max...@li... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss |