Reputation: 688
0 5 360 5 mul {sin} for
stack
quit
I understand that we have a for loop with a range from 0 --> 360 with increments of 5. However, the 5 mul
is confusing.Why is it before the for loops body? The 5 mul
is applied to what? Where is the first argument? i.e: 4 5 mul
which is 4 x 5 = 20.
Upvotes: 0
Views: 66
Reputation: 160251
(Not an answer, but need formatting.)
PostScript is a stack-oriented language. Operators take the number of arguments they expect from the stack. You can play with this in an interpreter like Ghostscript:
GS> 5 360 mul
GS<1> stack
1800
At this point the stack is [1800].
If you provide more arguments than an operator takes those values are still on the stack waiting for something else to happen:
GS> 0 5 360 5 mul
GS<3> stack
1800
5
0
Since mul
takes two values it will just pop 5
and 360
, do the multiplication, and push the answer onto the stack. I.e., before the mul
, your stack is this:
GS> 0 5 360 5
GS<4> stack
5
360
5
0
If you did a mul
now you'd end up with the same thing:
GS<4> mul
GS<3> stack
1800
5
0
Then the code continues on as before, with the {sin} for
.
You can do all the commands at once, as you show, or do it later--the stack is just the stack: it's modified as you go. So whether you do it like this:
GS> 0 5 360 5 mul { sin } for
GS<361>
Or
GS> 0 5 360 5
GS<4> mul
GS<3> { sin }
GS<4> for
GS<361>
you'll end up with the same thing.
In GhostScript (gs) the number in <nnn>
is the current stack depth.
Stack-based languages can be a bit confusing if you're not used to the paradigm (or older HP calculators :) Forth is the canonical example of a stack-based language (and it's awesome). I miss the Forth and PostScript days (I used to write printers, and used Forth heavily in embedded systems). There are more-modern stack-based languages as well (Kitten, Factor).
Upvotes: 1
Reputation: 347
The first argument is — guess what — 360:
0 5 1800 {sin} for stack quit
PostScript is stack oriented, and there's no special syntax for the for
loop. You can therefore specify any expressions for the first three values. This would make it iterate 360 times, from 0 to 1800 (which is 360×5), with the step of 5. I don't know why it does this, though.
Upvotes: 4