Re: Add syntax highlighting to own command

• To: mathgroup at smc.vnet.net
• Subject: [mg101652] Re: Add syntax highlighting to own command
• From: Daniel <janzon at gmail.com>
• Date: Sun, 12 Jul 2009 06:57:24 -0400 (EDT)
• References: <h340tk\$guu\$1@smc.vnet.net>

```Hello Bastian,

I'm certainly unable to make the construct better, but I can show how
I would do it. I'm also fairly new to Mathematica, so don't use my
code in 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}

Do our solutions differ in behavior? I wouldn't be surprised at all if
our codes behaved differently in the details.

All the best,
Daniel Janzon

On Jul 9, 7:58 am, 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

```

• Prev by Date: Re: Data conversion
• Next by Date: Re: Add syntax highlighting to own command
• Previous by thread: Re: Add syntax highlighting to own command
• Next by thread: Re: Add syntax highlighting to own command