gpt4 book ai didi

Clojure。丢弃每个?

转载 作者:行者123 更新时间:2023-12-05 08:43:49 24 4
gpt4 key购买 nike

Clojure 库是否具有“drop-every”类型的函数?采用惰性列表并返回每删除第 n 个项目的列表的东西?

不太明白怎么做。

干杯

菲尔

最佳答案

(defn drop-every [n xs]
(lazy-seq
(if (seq xs)
(concat (take (dec n) xs)
(drop-every n (drop n xs))))))

例子:

(drop-every 2 [0 1 2 3 4 5])
;= (0 2 4)

(drop-every 3 [0 1 2 3 4 5 6 7 8])
;= (0 1 3 4 6 7)

作为旁注,drop-nth 将是一个诱人的名字,因为 clojure.core 中已经有一个 take-nth .但是,take-nth 总是返回第一个项目,然后返回之后的每个第 n 个项目,而上述版本的 drop-every 从第 n 个项目开始删除第 n 个项目原始序列。 (删除第一项和第一项之后的每第 n 项的函数将根据上述内容直接编写。)

关于Clojure。丢弃每个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212445/

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