Questions tagged [coding-style]

Questions on how to write code in a better or different style, using Mathematica's capabilities for coding in multiple styles.

Mathematica differs from other programming languages in that it allows users to write their code in a variety of styles.

  • Procedural programming of the kind familiar to users of C or Fortran. The canonical constructs of this style are the Do and For loop and it is heavily relying on saving intermediate values into variables. This is the kind of programming most novice Mathematica users resort to, and it is usually the least efficient.
  • Functional programming is considered more canonically "Mathematica-style". Familiar to uses of LISP. The typical constructs in functional programming are Map (/@), Apply (@@), MapThread and pure functions (Function) using Apply and Slot (#, &).
  • Rule-based programming uses Mathematica's pattern-matching engine to match and rewrite expressions. A typical construction in this style is the replacement rule e.g. a -> b or a :> b, which is involved using Replace, ReplaceAll (/.) or ReplaceRepeated (//.).
155 questions
60
votes
5 answers

Placement of Condition /; expressions

It is my practice to place Condition expressions on the left side of := and :> in almost every case. I find this to be more logical as it is part of the pattern With the exception of use inside Module, Block, or With on the RHS, which is a special…
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
48
votes
2 answers

Granular versus terse coding

While reading Leonid's grand answers to General strategies to write big code in Mathematica? I came across something that goes against my own practices. I do not disagree with the principle but the degree to which it is taken feels both alien and…
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
18
votes
1 answer

Is there a guideline about when to end expressions with semicolons?

Whether for nested Ifs or within scoping constructs like Module and Block, I never know whether to end the final expression with a semicolon. I know the semicolon suppresses output, but, for example, in a series of nested Ifs, one needs only to…
Andrew Cheong
  • 3,576
  • 17
  • 42
10
votes
3 answers

Are these nested Tables necessary?

In exploring this fish population game with my calculus students, I wrote some Mathematica code to search for the sequence of catches that would result in the maximal total population of fish (both in the pond and in the boats) after ten days. The…
Justin Lanier
  • 215
  • 1
  • 5
6
votes
1 answer

Proper style for function documentation?

Is there a good convention for documenting functions/return values? If I just put them in comments on top of function, the alignment is all over the place.
Yaroslav Bulatov
  • 7,793
  • 1
  • 19
  • 44
2
votes
0 answers

Coding style/formatting rules used by Wolfram developers

I wondered whether there is a known formatting guide that Wolfram employees (have to) use when writing Mathematica code? As stated in Mathematica style guide? there are many different formatting conventions one can experiment with but I would like…
Gert
  • 1,530
  • 8
  • 22
2
votes
0 answers

How to calculate how many points in function f are greater than the average of functions g and k?

Consider 3 functions f, g and k Suppose we want to know for 3. functions from P={(x, y )|x=-1,-0.9,-0.8,-0.7....,1 and y=-1,-0.9,-0.8,-0.7....1 this is what i have so far Clear[f, x, y, g, k] f[x_, y_] := x^2+2*Sin[Cos[x*y]]-2 g[x_,…
Aran
  • 373
  • 1
  • 9
1
vote
2 answers

Need help with making code work on Mathematica

Table[g = Map[Sort, Solve[a + b + c == L && a^2 + b^2 == c^2 && a > 0 && b > 0 && c > 0, Integers] // Values] // DeleteDuplicates, {L, 12, 3000}] Count[g] this is the question, its suppose to be written in…
Aran
  • 373
  • 1
  • 9
1
vote
0 answers

Postfix operator //, when to use it

The postfix operator // seems more legible when applying formatting functions or functions that provide general information about an object, like Length does. Is it acceptable to use it in those cases? Example code: StringJoin[ "Elements in Range…
Gladaed
  • 987
  • 4
  • 12
0
votes
1 answer

Loop for different values

I have the following: ClearAll["Global`*"] n= 3; T = Transpose[Table[{t^i}, {i, 0, n}]]; B = DiagonalMatrix[Range[n], 1, n + 1]; Print[ T, B] I want to have a loop for different values of n say n=1 to 100, so I can get T and B.
user62716
  • 731
  • 4
  • 11
0
votes
1 answer

How can I add the tangent line from Fun 2 to Fun 3 to this function?

Question: I need to move the point Fun1 to 10 positions in the interval [-3,0), and have the drawing updated each time (including the tangent and intersection points). I don't know how to add Fun2-Fun3 to the drawing. Is what is done on the bottom…
Qy Ln
  • 69
  • 5
0
votes
1 answer

Find the area which is between two curves using Nintegrate

eq1 = (x^2/400) + (y^2/256) == 1; eq2 = (x^2/144) - (y^2/289) == 1; sl = Solve[{eq1, eq2}, {x, y}] NIntegrate[eq1 - eq2, {x, s1[1], s1[2]}] NIntegrate[eq1 - eq2, {x, s1[2], s1[3]}] NIntegrate[eq1 - eq2, {x, s1[3],…
Aran
  • 373
  • 1
  • 9
0
votes
1 answer

Question about finding tangents on mathematica?

How can I find the tangent at the point $(2,8)$ to the curve $y=x^2+4$ on Mathematica? How can I show the tangent of the curve at the point and I need to mark the point where they meet with the word "FunOne" f[x_] := x^2+4 p = Plot[f[x], {x, 2, 8},…
Aran
  • 373
  • 1
  • 9