定义一个回文运算符,它在后记中以相反的顺序复制堆栈中的值

Define a palindrome operator that duplicates the values on the stack in reverse order in postscript

定义一个回文运算符,以相反的顺序复制堆栈中的值。

这是我目前所拥有的,它没有按照我的意愿去做

/palindrome { 1 dict begin count 1 gt { /first exch def /second exch def temp1 = first temp2 = last first = last last = temp1 } } def

你在那里写的大部分内容在 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

所以总的来说,我希望这个 PostScript 函数将一个布尔值压入操作数堆栈,true 或 false 取决于堆栈在执行时是否至少有 2 个对象,然后是一个可执行数组到操作数栈并退出。

如果我这样做,我会将堆栈存储到一个数组中,然后将数组卸载回堆栈,然后从头到尾遍历数组。类似于:

%!

/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

也可以(我这里有一个工作示例)通过使用 for 循环和操作堆栈一次性完成此操作,而无需定义任何额外的存储对象(字典或数组)。这对我来说似乎是一个更优雅的解决方案,留作 reader :-)

的练习