如何访问函数返回的多个值(例如,cl:parse-integer)?

时间:2014-11-03 02:49:16

标签: lisp common-lisp multiple-value

我正在尝试从字符串中获取三个数字

(parse-integer "12 3 6" :start 0 :junk-allowed t)
12 ;
2

现在这也返回2,这是可以解析它的数字。 所以我现在可以给出

(parse-integer "12 3 6" :start 2 :junk-allowed t)
3 ;
4

但是如何存储它返回的24的值。如果我将setq变为变量,则仅存储123

1 个答案:

答案 0 :(得分:11)

请阅读"理论" here

简而言之,您可以将multiple valuesmultiple-value-bind绑定:

(multiple-value-bind (val pos) (parse-integer "12 3 6" :start 0 :junk-allowed t)
  (list val pos))
==> (12 2)

您还可以setf多个values

(setf (values val pos) (parse-integer "12 3 6" :start 0 :junk-allowed t))
val ==> 12
pos ==> 2

另见VALUES Forms as Places

PS。在您的特定情况下,您可能只是

(read-from-string (concatenate 'string 
                               "("
                               "12 3 6"
                               ")"))

并获取列表(12 3 6)。 这不是最有效的方法(因为它分配了不必要的内存)。

PPS参见:

  1. How to convert a string to list using clisp?
  2. In lisp, how do I use the second value that the floor function returns?
相关问题