Tag Info

New answers tagged

1

This seems to work. q = "x"; w = "y"; kk[] := Module[{q, w, m}, q[a_] := a + 1; w[a_] := a^2; m[Symbol["q"]] = q; m[Symbol["w"]] = w; m] Cant imagine why you'd want to do this though.


8

Module works different than scoping constructs in other languages. Here's a simpler example which already gives a clue of what happens: x=3; Module[{x}, {x, Global`x, Context[x]}] (* ==> {x$81, x$81, Global`} *) You see, no matter whether you prefix it with Global`, x gets replaced with x$81, which indeed also has global context. Indeed, this is how ...


2

I am quite unable to understand why you just don't write kk[q][a_] := a + 1 kk[w][a_] := a^2 This gives {kk[q][1], kk[w][2]} {2, 4} Wouldn't the above satisfy your needs?


2

I think your requirement is not correct for the user. What should display on the screen should match the current x+y value based on what is currently selected for x and y and not what was there before. If you keep the old value displayed, then the new selection do not match what is on the screen and that can be confusing. But I made two versions, and you ...


2

Use bookmarks to return to your initial settings or to any other bookmarked settings.


3

Why not just take advantage of built-in, faster means? E.g. to create say five streams of "bits" each with specified transition probabilities: RandomFunction[DiscreteMarkovProcess[1, {{.2, .8}, {.6, .4}}], {0, 20}, 5]["States"] - 1 (* {{0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0}, {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, ...


2

I think this does what you're trying to accomplish. Note, you're not using "pure functions" anywhere in your code, I just jiggered it to get what I think you're after, there are almost certainly cleaner ways to do what I think you're trying to do (like actually using pure functions, etc.) tempMakeTableAn[{analEqs_, concs_, initConcs_, rateConstants_, ...


2

You are seeing Module work exactly as it should, because it is designed to implement lexical scoping. Take it away, Wikipedia: In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable ...


0

Nothing special here, except it uses your idea of encoding the card using a base-13 representation: card[n_Integer /; 1 <= n <= 52] := With[{suits = {"Hearts", "Diamonds", "Clubs", "Spades"}, values = Join[{"Ace"}, ToString /@ Range[2, 10], {"Jack", "Queen", "King"}]}, IntegerDigits[n - 1, 13, 2] /. {s_, v_} :> values[[v + 1]] ...


0

randomHand[x_] := Module[{cards, suits, value}, cards = DeleteCases[#, {___, 0}, 2] &@ PadLeft[IntegerDigits[RandomInteger[{1, 52}, x], 13]]; suits = First /@ cards /. {0 -> "Hearts", 1 -> "Diamonds", 2 -> "Clubs", 3 -> "Spades"}; value = Last /@ cards /. {11 -> "Jack", 12 -> "Queen"}; StringJoin @@ Insert[ToString /@ #, ...


5

suits = {" of Hearts", " of Diamonds", " of Clubs", " of Spades"}; deck = Join @@ Outer[StringJoin, Join[ToString /@ Range[2, 10], {"Jack", "Queen", "King", "Ace"}], suits]; RandomSample[deck, 5] (* {"King of Diamonds", "2 of Spades", "6 of Hearts", "5 of Clubs", "8 of Diamonds"} *)


4

It is because ResetButton refers to Manipulate`s initial state while pt is outer DynamicModule variable here. You can scope variables in Manipulate with a cool trick, which I've learned here: {{pt, {0.5, 0.5}}, None} Manipulate[ ArcTan @@ pt, {{pt, {0.5, 0.5}}, None}, DynamicModule[{}, LocatorPane[ Dynamic[pt], ...


9

You need to use Module option on return myMod := Module[{i}, Do[ Return[1, Module], {i, 3} ]; Print["test"] ] and now myMod (* 1 *) This is because Return only returns from nearest enclosure, which was Do in your case and not from the whole Module unless you use the Module second option to Return. This is different from other languages ...



Top 50 recent answers are included