对于Euler项目Problem 8,我被告知需要解析一个1000位的数字。 这是一个蛮力的Lisp解决方案,它基本上每隔5个连续的数字进行一次,并将它们从开始到结束相乘,并返回循环结尾的最大数字。Common Lisp:“字符串中没有非空白字符”
代码:
(defun pep8()
(labels ((product-of-5n (n)
(eval (append '(*)
(loop for x from n to (+ n 5)
collect (parse-integer
1000digits-str :start x :end (+ x 1)))))))
(let ((largestproduct 0))
(do ((currentdigit 0 (1+ currentdigit)))
((> currentdigit (- (length 1000digits-str) 6)) (return largestproduct))
(when (> (product-of-5n currentdigit) largestproduct)
(setf largestproduct (product-of-5n currentdigit)))))))
它编译没有任何警告,但在运行它,我得到:
no non-whitespace characters in string "73167176531330624919225119674426574742355349194934...".
[Condition of type SB-INT:SIMPLE-PARSE-ERROR]
我检查,看看是否本地函数product-of-5n
被写它的工作再作为全局函数:
(defun product-of-5n (n)
(eval (append '(*)
(loop for x from n to (+ n 5)
collect (parse-integer
1000digits-str :start x :end (+ x 1))))))
编译时没有警告和upo运行它似乎运行得很好。例如,
CL_USER>(产品的-5N 1)=> 882
由于第一5个位数为7,3,1,6和7
这似乎是正确的至于1000digits-str
,有人简单地用defvar
编译并与Emacs的longlines-show-hard-newlines
,我不认为有在字符串中的任何空白字符,因为这是SBCL抱怨,对不对?
使用EVAL是不好的。尝试替换它。 – 2012-07-18 19:17:20