【问题标题】:How to access multiple values returned by a function (e.g., cl:parse-integer)?如何访问函数返回的多个值(例如 cl:parse-integer)?
【发布时间】:2014-12-29 16:29:01
【问题描述】:

我试图从一个字符串中取出三个数字

(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

【问题讨论】:

    标签: lisp common-lisp multiple-value


    【解决方案1】:

    请阅读“理论”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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多