Loops 在Common Lisp中,如何使用依赖于较短列表的映射循环较长的列表以将某些函数应用于较长的列表?
基本上,我希望在Common Lisp中使用与此代码等效的代码,但最好使用更方便的方式Loops 在Common Lisp中,如何使用依赖于较短列表的映射循环较长的列表以将某些函数应用于较长的列表?,loops,functional-programming,mapping,lisp,common-lisp,Loops,Functional Programming,Mapping,Lisp,Common Lisp,基本上,我希望在Common Lisp中使用与此代码等效的代码,但最好使用更方便的方式 (defun circular (items) (setf (cdr (last items)) items)) 我认为你的代码已经足够好了。 (唯一的问题是你) 您需要做的只是恢复您创建的列表: (defun map-circular (result-type function governing-sequence &rest reused-lists) "Apply `fun
(defun circular (items)
(setf (cdr (last items)) items))
我认为你的代码已经足够好了。 (唯一的问题是你) 您需要做的只是恢复您创建的列表:
(defun map-circular (result-type function governing-sequence &rest reused-lists)
"Apply `function` to successive sets of arguments in which one argument is obtained from each sequence.
The result has the length of `governing-sequence`."
(let ((last-cells (mapcar (lambda (list)
(let ((cell (last list)))
(setf (cdr cell) list)
cell))
reused-lists)))
(unwind-protect
(apply #'map result-type function governing-sequence reused-lists)
(dolist (cell last-cells)
(setf (cdr cell) nil)))))
测试:
如果使用SERIES软件包,则可以使用SERIES功能,该功能: 创建一个无限系列,该系列无休止地重复中的给定项 命令发出了 如果这些项是静态已知的,即不是从运行时给定的值列表中知道的,则此方法有效 下面的代码扫描一个列表,同时遍历无限系列1 2 3。。。。这两个序列与
映射
并行迭代,如s
和n
,映射生成一系列(cons s n)
。结果收集为一个列表:
(collect 'list
(mapping ((s (scan 'list '(a b c d e f g h)))
(n (series 1 2 3)))
(cons s n)))
结果是:
((A . 1) (B . 2) (C . 3) (D . 1) (E . 2) (F . 3) (G . 1) (H . 2))
系列采用流融合,扩展代码为:
(let* ((#:out-823 (list 1 2 3)))
(let (#:elements-816
(#:listptr-817 '(a b c d e f g h))
#:items-821
(#:lst-822 (copy-list #:out-823))
#:items-826
(#:lastcons-813 (list nil))
#:lst-814)
(declare (type list #:listptr-817)
(type list #:lst-822)
(type cons #:lastcons-813)
(type list #:lst-814))
(setq #:lst-822 (nconc #:lst-822 #:lst-822))
(setq #:lst-814 #:lastcons-813)
(tagbody
#:ll-827
(if (endp #:listptr-817)
(go series::end))
(setq #:elements-816 (car #:listptr-817))
(setq #:listptr-817 (cdr #:listptr-817))
(setq #:items-821 (car #:lst-822))
(setq #:lst-822 (cdr #:lst-822))
(setq #:items-826 ((lambda (s n) (cons s n)) #:elements-816 #:items-821))
(setq #:lastcons-813 (setf (cdr #:lastcons-813) (cons #:items-826 nil)))
(go #:ll-827)
series::end)
(cdr #:lst-814)))
如果需要将任意列表传递给函数,可以通过调用(NCOC list list)
创建自己的循环列表,这更易于编写。由于您可能不想更改输入列表,因此可以编写以下代码:
(defun circularize (list)
(let ((list (copy-list list)))
(nconc list list)))
(let* ((#:out-823 (list 1 2 3)))
(let (#:elements-816
(#:listptr-817 '(a b c d e f g h))
#:items-821
(#:lst-822 (copy-list #:out-823))
#:items-826
(#:lastcons-813 (list nil))
#:lst-814)
(declare (type list #:listptr-817)
(type list #:lst-822)
(type cons #:lastcons-813)
(type list #:lst-814))
(setq #:lst-822 (nconc #:lst-822 #:lst-822))
(setq #:lst-814 #:lastcons-813)
(tagbody
#:ll-827
(if (endp #:listptr-817)
(go series::end))
(setq #:elements-816 (car #:listptr-817))
(setq #:listptr-817 (cdr #:listptr-817))
(setq #:items-821 (car #:lst-822))
(setq #:lst-822 (cdr #:lst-822))
(setq #:items-826 ((lambda (s n) (cons s n)) #:elements-816 #:items-821))
(setq #:lastcons-813 (setf (cdr #:lastcons-813) (cons #:items-826 nil)))
(go #:ll-827)
series::end)
(cdr #:lst-814)))
(defun circularize (list)
(let ((list (copy-list list)))
(nconc list list)))