将字符串列表转换为 Common Lisp 中的整数列表
Converting list of strings to list of ints in Common Lisp
我有一行 "fun,arg1,arg2" <- 它是一个字符串
我用“,”分隔符将这个字符串拆分为字符串列表。然后我将 "fun" 与一些字符串进行比较(例如 "Fibonacci")。
拆分函数(工作正常)
(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
没有任何改变;它 returns 使用 tab
.
的新列表
我有一行 "fun,arg1,arg2" <- 它是一个字符串
我用“,”分隔符将这个字符串拆分为字符串列表。然后我将 "fun" 与一些字符串进行比较(例如 "Fibonacci")。
拆分函数(工作正常)
(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
没有任何改变;它 returns 使用 tab
.