Re: Add syntax highlighting to own command
- To: mathgroup at smc.vnet.net
- Subject: [mg101658] Re: Add syntax highlighting to own command
- From: Daniel <janzon at gmail.com>
- Date: Sun, 12 Jul 2009 06:58:34 -0400 (EDT)
- References: <h340tk$guu$1@smc.vnet.net>
Hello Bastian, I'm certainly unable to make a better construct, but I can show how I would do it. I'm also fairly new to Mathematica, so don't use my code for automated brain surgery... mywith[{}, expr_] := expr mywith[{head_, tail___}, expr_] := With[{head}, mywith[{tail}, expr]] SetAttributes[mywith, HoldAll] Test: (* Input *) mywith[{a = "Hi, ", b = a <> "there"}, Print[b]] mywith[{a = 1, b = a + 1, c = b + 1, d = a + b + c}, {a, b, c, d}] (* Output *) Hi, there {1, 2, 3, 6} Which agrees with your Let function. Do our functions behave differently in some way? Probably, it would be instructing to sort it out. All the best, Daniel Janzon PS! (This may be a double post, but I think I accidently pressed "Discard" instead of "Send" so this is a second try. Sorry it there is any trouble.) On 9 Juli, 07:58, earth... at web.de (Bastian Erdnuess) wrote: > I was missing a scoping construct like 'With' but where the local > variables get assigned linearly. E. g. > > In[1] := LinearWith[ { > a = "Hi, ", > b = a <> "there!" }, > Print[ b ] ] > > Out[1] = "Hi, there!" . > > I'm fairly new to Mathematica, but I know some Lisp, and somehow I got > it. I called my new construct Let and defined it as: > > Let[ vars_, expr_ ] := > If[ Length[ Unevaluated[ vars ] ] == 1, > With[ vars, expr ], > (* else *) > Unevaluated[ vars ] /. > { a_, b__ } :> > With[ { a }, > Let[ { b }, expr ] ] ] > > SetAttributes[ Let, HoldAll ] > > It seems to work fine so far. > > Now, I would like to have this construct load always when I start > Mathematica and I don't want to get it cleared when I use > 'Clear["Global`*"]'. So I put it in the System` context and also added > the attribute 'Protected'. I wrote all in a file Let.m and now, I > wonder where to put it that it gets read automatically at each startup > of Mathematica. > > However, my first question: Is it a bad idea to add things to the > System` context? And if not, where to put my file? And if, what wou= ld > be better? > > Second, my main question: Is it somehow possible to add this nice syntax > highlighting to the Let construct like with the With construct, where > the local variables appear in green? > > Third: Can I somehow add a help page? I have already the Let::usage. > > And last: Does anybody know how to make the construct better? Is there > something like syntax transformation rules in Mathematica? > > Thank you for your answers, > Bastian