Sow the phase and Reap the sin -- why?

  • Topic: Mathematica 
  • Thread starter Swamp Thing
  • Start date
  • Tags Tags
    Phases Sine
2 replies · 3K views · last reply
Swamp Thing
Insights Author
Messages
1,079
Reaction score
849
Mathematica code :

Code:
Reap[     (
              sin = (
                           (
                             phase = 2 Pi #/32; Sow[phase]; Sin[phase]
                           ) & /@ Range[32]
                    )
           )
     ]

The output looks like this:
Mathematica output of a nested list: a 4x8 grid of Sin[k pi/16] and Sin[k pi/8] values, then a row of 1, Cos and 0 values, then negated sine and cosine entries, followed by a sublist of angles from pi/16 up to 2 pi in steps of pi/16.



But I would have expected only the phases, i.e. the second sublist only. Why do the sine values get reaped along with the phases that are actually sown?
 
Reap returns a list with two elements. The first is the value of the expression evaluated. The second is the list of sown elements.
 
Ok... a semicolon after the "sin = (...)" changes the sin arrray into a Null. In the past I have sometimes wondered about the Null... I suppose I used to put the semicolon in without realizing its role, and forgot to do so this time. Also this time, the reaping of the sin values is actually better than assigning them inside the loop. So I will remove the semicolon and the assignments and then unpack the big list that gets reaped, containing the phases as well as the sin terms.

Thank you.