gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:30 25 4
gpt4 key购买 nike

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

(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

最佳答案

请看《理论》here .

简而言之,您可以绑定(bind) 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 .

附言。在您的特定情况下,您可能只是这样做

(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?

关于lisp - 如何访问函数返回的多个值(例如,cl :parse-integer)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com