在Common Lisp中将字符串列表转换为整型列表



我有一行像"fun,arg1,arg2" <-它是一个字符串

我用","分隔符将这个字符串分割成字符串列表。然后我将"fun"与一些字符串(例如:"斐波那契")。

拆分函数(工作正常)

(defun split-str (string &optional (r nil))
  (let ((n (position "," string
                     :from-end t
                     :test #'(lambda (x y)
                               (find y x :test #'string=)))))
    (if n
        (split-str (subseq string 0 n)
                   (cons (subseq string (1+ n)) r))
        (cons string r))))

测试函数

(defun tmp (a)
  (if (string= (nth 0 a) "Fibonacci")
    (progn
      (setf tab '())
      (dolist (n (cdr a))
        (cons tab '(parse-integer n))) ; parsing works fine (checked with write)
      (write tab)) ; always NIL
            ;(apply #'parse-integer (apply #'values a)) - doesn't work
    (write "nok")))
调用:

(tmp (split-str "Fibonacci,15,33"))

为什么我的标签没有2个元素?

cons不改变任何东西;它使用tab返回一个新的列表。

最新更新