Re: Add syntax highlighting to own command
- To: mathgroup at smc.vnet.net
- Subject: [mg101899] Re: Add syntax highlighting to own command
- From: earthnut at web.de (Bastian Erdnuess)
- Date: Tue, 21 Jul 2009 03:52:53 -0400 (EDT)
- References: <200907090600.CAA17547@smc.vnet.net> <h3766u$f9h$1@smc.vnet.net> <h41f80$rkq$1@smc.vnet.net>
Leonid Shifrin <lshifr at gmail.com> wrote: > Hi Bastian, > > If I understand you correctly and the last rule you added > > ( Let /: ( lhs_ := Let[ vars_, expr_ /; cond_ ] ) := > Let[ vars, lhs := expr /; cond ]) > > is to allow shared local variables in function definitions (like with > standard With, Module or Block), then your code does not work for me. Yes, you are (and understood me) right. > Consider the following definition: > [...] > f[x_, y_] := > Let[{xl = x, yl = y + xl + 1}, xl^2 + yl^2 /; (xl + yl < 15)]; I tried first something different than I actually posted. I thought then I could write it different. But somehow I must have forgotten to test it correctly. However, also with my original version (something like f_[ l_, Let[ r_ ] ] := bla[ l := r ] /; f == SetDelayed ) I would have gotten about f[ x$_, y$_ ] := x^2 + (1 + x + y)^2 /; 1 + 2x + y < 15 . I didn't recognized that the local variables in f get renamed. > I can think of several reasons why this does not work, and will expand this > discussion if you will be interested, but here is the version that I > believe does work: Now, I can too. But if you want to explain some of your reasons, i'll hear. I also found a trick to get around the renaming problem (using your Verbatim instead of mine f_[...] /; f == ...): Verbatim[ SetDelayed ][ l_, Let[ v_, Verbatim[ Condition ][ r_, c_ ] ] ] ^:= Let[ v, SetDelayed[ #, r /; c ] & @ ToExpression @ # ] & @ ToString[ l, InputForm ] This passes basically the left hand side as string around the issue with the renaming. The last days I often figured out that that what I think of as "syntax transformation" or Simon called "passing unevaluated arguments around" works best by passing the things around as strings. Are there disadvantages doing that? I could imagine that this might be not the best if one wants to compile the source -- but so far I'm far away from starting to try to understand Mathematicas compiler. > [ Block trick ] > This uses the Block trick. It is actually a true macro, since > it expands the code before SetDelayed acts on it: That seems to be a nice trick. Thank you for showing that. I need to think a little bit about that to decide if I like it for this situation. Regards, Bastian
- References:
- Add syntax highlighting to own command
- From: earthnut@web.de (Bastian Erdnuess)
- Add syntax highlighting to own command