如何修改函数内的列表

How to modify list inside a function

(defun list-parser (list count)
  ...);;this function reads items by count from list and do some process to them.

;;i.e.convert items read from code to char, or to other things and then return it. 

;;Also, the items in list should be consumed, globally. 

(defmethod foo ((obj objtype-2) data-list)
  (setf (slot-1 obj) (read-list data-list 1))
  obj)

(defmethod foo ((obj objtype-1) data-list)
  (setf (slot-1 obj) (read-list data-list 1)
        (print data-list)
        (slot-2 obj) (read-list data-list 2)
        (print data-list)
        (slot-3 obj) (foo (make-instance 'objtype-2) data-list)
        (print data-list)
        (slot-4 obj) (read-list data-list 3))
  obj)

如何让它像这样工作:(读取列表在某些方面就像读取字节一样工作:

1.return 一个值读取(并在此处解析)

2.change 流位置(这里是列表)。

(let ((obj)
      (data))
  (setf data '(1 2 3 4 5 6 7 8)
        obj (foo (make-instance 'objtype-1) data))
  (print data))
>>(2 3 4 5 6 7 8)
>>(4 5 6 7 8)
>>(5 6 7 8)
>>(8)

或者更确切地说,你是如何处理这种任务的?您是否将列表转换为其他类型?

我不太确定你在找什么,但这是一个创建 'list reader' 对象的函数(只是一个函数)。列表 reader 可让您读取列表的块,将其视为流。

(defun make-list-reader (l)
  ;; Make a list reader which, when called, returns three values: a
  ;; chunk of list, the length of tha chunk (which may be less than
  ;; how much was asked for) and the remaining length.  The chunk is
  ;; allowed to share with L
  (let ((lt l)
        (len (length l)))
    (lambda (&optional (n 1))
      (cond
       ((zerop len)
        (values nil 0 0))
       ((< len n)
        (values lt len 0))
       (t
        (let ((it (subseq lt 0 n)))
          (setf lt (nthcdr n lt)
                len (- len n))
          (values it n len)))))))

(defun read-from-list-reader (r &optional (n 1))
  ;; Read from a list reader (see above for values)
  (funcall r n))

现在:

(defvar *l* (make-list-reader '(1 2 3)))
*l*

> (read-from-list-reader *l* 1)
(1)
1
2

> (read-from-list-reader *l* 2)
(2 3)
2
0

> (read-from-list-reader *l* 10)
nil
0
0

你不能真正做的是编写一个函数(当然不是真正的函数,因为它修改了它的参数),它在修改它的参数列表时像这样工作。所以你可以写一个函数来做这个:

> (let ((l (list 1 2)))
    (values (read-from-list l)
            l))
(1)
(2)

它通过修改 l 的第一个缺点的 carcdr 来工作,正如您所期望的那样。但是,当没有更多内容可读时,这是行不通的:l 是缺点,而 nil 不是缺点,因此您永远无法使 l nil 有一个功能。

但无论如何,这样的函数对于粗心的人来说只是一大堆陷阱,而且通常很可怕:例如,您的示例将涉及修改文字,这是不合法的。