Tag Info

New answers tagged

3

You have already seen that there are a number of ways to skin this cat but I'd like to add some comments of my own. Other approaches When possible I prefer to avoid these situations in the first place, instead using something like: {{1, 2}, {3, 4}, {5, 6}} /. {x_, y_} :> Thread@{x, {y, y^2, y^3}} // Thread {{{1, 2}, {3, 4}, {5, 6}}, {{1, 4}, {3, ...


3

I'll just aggregrate here the various responses from the comments, plus my own humble thoughts and understanding (which may lack precision or contain mistakes, so feel free to improve or correct): Be careful about variable naming. (This is assuming that, generally speaking, you have good reason to have global variables spilling out in your program.) Use ...


1

This answer is also not direct answer because there is no Module or While but it is good to know that construct: f[n_Integer /; n>0] := f[n] = f[n - 1]/(6 n) + n! f[0] = 7 More here


3

Since you are interested in learning different ways to write this, here is one that avoids using "variables" entirely, thus removing the need for Module: fnw[i_Integer] := Last @ NestWhile[{# + 1, 1/(6 #) #2 + #!} & @@ # &, {1, 7}, #[[1]] <= i &] An expression such as #[[1]] <= i & is a pure function with a single parameter ...


3

You mean this? fw[i_Integer] := Module[{n = 0, last = 7}, While[++n <= i, last = 1/(6 n) last + n!; ]; last]


4

As in my comment above, here's a way that works even if a and/or i have global values: getA[f_, total_] := Module[{result}, result = Solve[Integrate[f[a, i], {i, 0, 255}] == total, a]; If[Length[result] == 0, Return[False]]; a /. First@result // N ]; plotShow[f_, max_] := Block[{a, i}, a = getA[f, max]; If[! a, Return[]]; Print[f[a, ...


2

If you put Read[str, Record, 1] in before, it will align the stream with the next record. Pane[Column /@ Dynamic[ReadList[str, Record, 1]; ReadList[str, Record, 200]], ImageSize -> {400, 200}, Scrollbars -> True] Does that help? (Tested with infil = "/usr/share/dict/words"). If it's a small file, you could preprocess an index of record ...



Top 50 recent answers are included