Define a palindrome operator that duplicates the values on the stack in reverse order.
this is what i have so far and it not doing what i want it to do
/palindrome {
1 dict begin
count 1 gt
{
/first exch def
/second exch def
temp1 = first
temp2 = last
first = last
last = temp1
}
} def
Most of what you have written there doesn't make any sense in PostScript:
/palindrome
{
1 dict begin
count 1 gt
{
/first exch def
/second exch def
%% The following four lines are not valid PostScript
temp1 = first
temp2 = last
first = last
last = temp1
%% There is no '=' assignment operator in PostScript, in PS the = operator
%% pops one object from the stack and writes a text representation to stdout.
%% You have not defined any of the keys temp1, temp2 or last
%% in any dictionary. If executed I would expect this program to throw an
%% 'undefined' error in 'temp1'
}
%% Given the 'count 1 gt' at the opening brace, I would expect this executable
%% array to be followed by a conditional, such as 'if'. Since it isn't this just
%% leaves the executable array '{....}' on the stack
} def
So overall I would expect this PostScript function to push a boolean on to the operand stack, true or false depending on whether the stack has at least 2 objects on it at the time of execution, and then an executable array on to the operand stack and exit.
If I were doing this I would store the stack into an array, then unload the array back to the stack and then iterate through the array from end to beginning. Something like:
%!
/palindrome
{
count array astore
aload
dup length 1 sub -1 0 {
1 index exch get
exch
} for
pop
} def
(line 1)
2
3
(before palindrome\n) print
pstack
palindrome
(after palindrome\n) print
pstack
It's also possible (I have a working example here) to do this in a single pass without defining any extra storage objects (dictionaries or arrays) by using a for loop and manipulating the stack. That seems like a more elegant solution to me, and is left as an exercise for the reader :-)