此lisp代码应采用常规数字及其罗马等效文件1=I
等文件,并从中创建一个点对(1 . I)
的大列,然后将该alist写入文件。< / p>
(defun arabic_to_roman (filename)
(let ((arab_roman_dp '())
(arab nil)
(roman nil)
(in (open filename :if-does-not-exist nil)))
(when in
(loop for line = (read-line in nil)
while line do
(setq arab (subseq line 0 (search "=" line)))
(setq roman (subseq line (1+ (search "=" line)) (length line)))
(setf arab_roman_dp (acons arab roman arab_roman_dp))
(format t "~S ~S~%" arab roman)))
(close in)
(with-open-file (stream #p"ar_out.txt"
:direction :output
:if-exists :overwrite
:if-does-not-exist :create )
(format stream arab_roman_dp))
;(format t "~S" arab_roman_dp)
))
在slime / sbcl中生成此错误:
in: DEFUN ARABIC_TO_ROMAN
; (FORMAT STREAM ARAB_ROMAN_DP)
;
; caught WARNING:
; Derived type of ARAB_ROMAN_DP is
; (VALUES LIST &OPTIONAL),
; conflicting with its asserted type
; (OR (VECTOR CHARACTER) (VECTOR NIL) BASE-STRING FUNCTION).
; See also:
; The SBCL Manual, Node "Handling of Types"
在我放入(format stream arab_roman_dp)
这是输入文件的示例:
1=I
2=II
3=III
4=IV=IIII
5=V
6=VI
7=VII
8=VIII=IIX
9=IX=VIIII
10=X
11=XI
12=XII
13=XIII
...
4994=MMMMCMXCIV
4995=MMMMCMXCV
4996=MMMMCMXCVI
4997=MMMMCMXCVII
4998=MMMMCMXCVIII
4999=MMMMCMXCIX
不确定我在这里做错了什么。基本上,我想把我创建的点对的大列表写到文件中。
((4999 . MMMMCMXCIX) (4998 . MMMMCMXCVIII) ... (1 . I))
答案 0 :(得分:1)
FORMAT的第二个参数必须是控制字符串。代码中的第二个参数arab_roman_dp
是一个列表。 SBCL可以检测到这个错误,并且正确发出此代码的警告信号。