Ghostscript で引数を読んで階乗を計算するスクリプト。括弧無し。
発端→http://e-arrows.sakura.ne.jp/2010/08/is-lisp-really-has-too-many-parenthesis.html
ということで、PostScript でやってみた。
#!/bin/sh true clear %; exec gs -q -dNODISPLAY -dBATCH -d#!=0 -- $0 $@ ARGUMENTS dup length 0 eq /pop cvx 1 1 array astore 2 array astore cvx if 0 get cvi dup =only <21203D20> print 1 exch -1 1 /mul cvx 1 array astore cvx for =
途中にある <21203D20> は "! = " の16進数表記。PostScript の文字列リテラルは () で括るため書けないので、代わりに 16進数表記を使った。< と > が括弧として使われているが、この 2 字はそう簡単には使用禁止にできまい。
使用禁止にしたところで対処方法はある。
#!/bin/sh true clear %; exec gs -q -dNODISPLAY -dBATCH -d#!=0 -- $0 $@ ARGUMENTS dup length 0 eq /pop cvx 1 1 array astore 2 array astore cvx if 0 get cvi dup =only currentfile 80 string readline ! = pop print 1 exch -1 1 /mul cvx 1 array astore cvx for =
追記: あ、「指定数まで」か。
#!/bin/sh true clear %; exec gs -q -dNODISPLAY -dBATCH -d#!=0 -- $0 $@ /n 1 def ARGUMENTS length 0 gt /n ARGUMENTS 0 /get cvx /cvi cvx /def cvx 6 array astore cvx if 1 1 1 n /dup cvx /=only cvx /mul cvx currentfile 80 string readline ! = pop /print cvx /dup cvx /= cvx 7 array astore cvx for