delgeezee
delgeezee

Reputation: 113

how to do a postscript recursive loop

i'm new to postscript. What is a format for a function that calls itself recursively. lets say I have a function called squares that prints out a square. 5 square // prints out 5 squares

I think 5 will be a the top of the stack. Each repititon will decrease that number until 0 is met. If there is an easier way to do this, let me know.

Upvotes: 2

Views: 888

Answers (2)

juFo
juFo

Reputation: 18577

/go 
{
    /cnt { 1 add dup == } def
    0
    { cnt } loop
} def

% start by calling go
go

a simple infinite counter that should you get started

Upvotes: 0

KenS
KenS

Reputation: 31159

%!PS-
%
% int myfunction -
% Executes recursively 'int' times
%
/myfunction {
  dup ==           % Print out the current 'int' just to show we're doing something
  1 sub            % decrement 'int' by 1
  dup 0 gt         % copy 'int' and test to see if its 0
  {                %   not 0, so recurse, the copy on the stack is the new 'int'
     myfunction    % execute recursion
  } {
    pop            %   The copy of 'int' was 0,so remove the copy from the stack
  } ifelse
} bind def

5 myfunction

Or you could just use loop to execute a code block 5 times.

Upvotes: 1

Related Questions