问题描述
定义一个回文运算符,以相反的顺序复制堆栈上的值。
这是我到目前为止所拥有的,它没有按照我想要的去做
// will return true if *any* of the mentions were you
if (message.mentions.users.has('Your ID'))
return message.channel.send('Do not mention this user');
/palindrome {
1 dict begin
count 1 gt
{
/first exch def
/second exch def
temp1 = first
temp2 = last
first = last
last = temp1
}
解决方法
您在此编写的大部分内容在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循环并操作堆栈,也可以一次通过(而不用定义任何额外的存储对象(字典或数组))来完成此操作(这里有一个有效的示例)。对于我来说,这似乎是一个更优雅的解决方案,留给读者练习:-)