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

shr*_*dur 7 lisp common-lisp multiple-value

我想从字符串中得到三个数字

(parse-integer "12 3 6" :start 0 :junk-allowed t)
12 ;
2
Run Code Online (Sandbox Code Playgroud)

现在这2也会返回,这是可以解析的数字.所以我现在可以给

(parse-integer "12 3 6" :start 2 :junk-allowed t)
3 ;
4
Run Code Online (Sandbox Code Playgroud)

但我怎么保存的价值24它返回.如果我setq是到一个变量只123存储?

sds*_*sds 11

请在这里阅读"理论" .

简单地说,你可以绑定多个值multiple-value-bind:

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

你也可以setf多个values:

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

另请参见VALUES Forms as Places.

PS.在您的特定情况下,您可能会这样做

(read-from-string (concatenate 'string 
                               "("
                               "12 3 6"
                               ")"))
Run Code Online (Sandbox Code Playgroud)

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

PPS参见:

  1. 如何使用clisp将字符串转换为列表?
  2. 在lisp中,如何使用floor函数返回的第二个值?