Re: Lisp Macros in Mathematica (Re: If Scheme is so good

*To*: mathgroup at smc.vnet.net*Subject*: [mg102545] Re: Lisp Macros in Mathematica (Re: If Scheme is so good*From*: David Bailey <dave at removedbailey.co.uk>*Date*: Thu, 13 Aug 2009 03:20:59 -0400 (EDT)*References*: <b0g665llur83sj9dnumktjvnipacj7bgrt@4ax.com> <h5r8ek$l71$1@smc.vnet.net>

David Bakin wrote: > It is very easy to make what Lisp calls "special forms". You use HoldAll or > related attributes to create your own "special forms", then manipulate the > arguments in their full form (aka S-expressions), evaluating things when you > wish. > > As far as I am aware you do not get reader syntax like quotes and > quasiquotes that make Lisp macros easy to write. But depending on what > you're trying to do you may not need them. With Mathematica you get full > pattern matching on arguments (not just destructuring) and rule-based > programming, and everything else that Mathematica provides. You may find > these features are even more effective than quoting and quasiquoting in > writing macros. > > BTW, Mathematica does not need a separate defmacro call that basically means > "define this function such that you don't evaluate any arguments, but when I > return the result, you evaluate it". That is because 1) To get the first > part you add the HoldAll attribute to your function name, and 2) Mathematica > automatically evaluates the result returned by any/all functions, until > there's nothing more to evaluate (which is an important difference between > the Lisp REPL and the Mathematica REPL). > If I understand what you mean, then surely this is missing the point. Let's take a specific example. Suppose you have a vector of data representing conditions in a reaction vessel, and you define functions such as: pH[x_]:=x[[17]]; This is a function, and could be used to access the relevant component of a vector, but not to change that value. However, if you code in that style, you will sacrifice a lot of performance for the sake of clarity. One way to solve that problem, is to define another operator for use in defining functions instead of SetDelayed (:=). This lets you make substitutions on the held form of the RHS before the DownValue is created - so you code pH[vec] but execute vec[[17]] . Alternatively, you could use $Pre or $PreRead, but since these do not work on code loaded by Get or Needs, I don't find this approach general enough. As I said before, I think a built-in macro mechanism would be a valuable addition to Mathematica. David Bailey http://www.dbaileyconsultancy.co.uk