gpt4 book ai didi

lisp - 使用步长对列表进行切片

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

我四处搜索但没有找到一个简单的答案。

如何在不使用 loop 的情况下使用给定的 step 对 Common Lisp 中的列表进行切片?subseq 不采用第三个参数,即 (subseq lst start end step) 似乎很奇怪。

Python 等价物:

 lst[start:end:step]

最佳答案

标准 CL 中没有类似内容。可以使用 REDUCEDO 来实现它。我只会使用 LOOP:

辅助函数:

(defun %take (it what)
(cond ((eq what :all) it)
((eq what :none) nil)
((and (numberp what) (plusp what))
(subseq it 0 what))
((and (numberp what) (minusp what))
(last it (- what)))
((and (consp what)
(= (length what) 1)
(numberp (first what)))
(nth (first what) it))
((and (consp what)
(= (length what) 2)
(numberp (first what))
(numberp (second what)))
(let ((end (if (minusp (second what))
(+ (length it) (second what))
(second what))))
(subseq it (first what) end)))
((and (consp what)
(= (length what) 3)
(numberp (first what))
(numberp (second what))
(numberp (third what)))
(let ((start (first what))
(end (if (minusp (second what))
(+ (length it) (second what))
(second what)))
(by-step (third what)))
(loop for e = (subseq it start) then (nthcdr by-step e)
for i from start below end by by-step
collect (first e))))))

采取:

(defun take (thing &rest description)
"Taking things from lists like in Mathematica
Description is one or more of:
:all | :none | [sign]number | ( start [end [step]])"
(cond ((null description) nil)
((and (consp description)
(= (length description) 1))
(%take thing (first description)))
(t (loop for e in (%take thing (first description))
collect (apply #'take e (rest description))))))

例子:

CL-USER 27 > (take '(0 1 2 3 4 5 6 7 8 9 10 11) '(2 7 2))
(2 4 6)

CL-USER 28 > (defun sublist (list start end step)
(take list (list start end step)))
SUBLIST

CL-USER 29 > (sublist '(0 1 2 3 4 5 6 7 8 9 10 11) 2 7 2)
(2 4 6)

关于lisp - 使用步长对列表进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39055248/

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