【问题标题】:Modify list and return it inside a function in lisp修改列表并将其返回到 lisp 的函数中
【发布时间】:2015-05-24 00:50:25
【问题描述】:
(defun testthis (node index newvalue)
  (set-nth node index newvalue)
   node
)

我想在一个函数中修改一个列表的第n个元素,然后返回这个列表来保存所做的修改。

如何在 lisp 中实现这样的功能?

【问题讨论】:

    标签: lisp elisp common-lisp


    【解决方案1】:

    您可以使用setf 和nth 访问器:

    (defun replace-index (node index new-value)
       (setf (nth index node) new-value)
       node)
    
    (defparameter *orig* (list 1 2 3 4))
    (defparameter *mod* (replace-index *orig* 2 99))
    (list *orig* *mod*) ; ==> ((1 2 99 4) (1 2 99 4))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      相关资源
      最近更新 更多