Manipulate inside module

366 views
Skip to first unread message

Charles Wells

unread,
Sep 10, 2011, 7:34:45 AM9/10/11
to
I expected the following expression to show the square of the current value of the slider, but it shows only a^2. There is clearly something I don't understand about Module. Any insights?

Module[{b = a}, (Print[b]; Manipulate[b^2, {{a, 1}, 0, 5}])]

Scot T. Martin

unread,
Sep 11, 2011, 7:28:17 AM9/11/11
to
Try:

Module[{b = a}, (Print[b]; Manipulate[b=a;b^2, {{a, 1}, 0, 5}])]

The issue is that Module[{b=a},...] only assigns b initially. You need to
update its value later.




________________________________________
From: Charles Wells [Cha...@abstractmath.org]
Sent: Saturday, September 10, 2011 07:30
Subject: Manipulate inside module

Bob Hanlon

unread,
Sep 11, 2011, 7:32:26 AM9/11/11
to
Manipulate[
Module[{b = a}, b^2], {{a, 1}, 0, 5, Appearance -> "Labeled"}]


Bob Hanlon

---- Charles Wells <Cha...@abstractmath.org> wrote:

=============

John Fultz

unread,
Sep 12, 2011, 4:23:13 AM9/12/11
to
On Sat, 10 Sep 2011 07:30:40 -0400 (EDT), Charles Wells wrote:
> I expected the following expression to show the square of the current
> value of the slider, but it shows only a^2. There is clearly something I
> don't understand about Module. Any insights?
>
> Module[{b = a}, (Print[b]; Manipulate[b^2, {{a, 1}, 0, 5}])]

I could explain in some considerable detail exactly what's going on here (and it
would be considerable), but it might be more useful and straightforward to give
you a very simple rule...

Module variables should *never* appear inside Dynamics or Manipulates internal
to that Module.

To be clear with some examples (all using Dynamic, but they could equally well
use Manipulate, which is implemented using Dynamic)...

(* OK *) Dynamic[Module[{a}, a]]
(* OK *) Module[{a},
(* expression involving a*);
Dynamic[(* expression *not* involving a *)]
(* BAD *) Module[{a}, Dynamic[a]]

In fact, this rule is so straightforward and universal, there's no reason that
we shouldn't have some advisory syntax highlighting which points out the
badness...I intend to do this for a future release.

Sincerely,

John Fultz
jfu...@wolfram.com
User Interface Group
Wolfram Research, Inc.

Reply all
Reply to author
Forward
0 new messages