gpt4 book ai didi

list - 替换子序列的函数

转载 作者:行者123 更新时间:2023-12-01 00:58:24 25 4
gpt4 key购买 nike

有没有可以替换子序列的函数?例如:

user> (good-fnc [1 2 3 4 5] [1 2] [3 4 5])
;; => [3 4 5 3 4 5]

我知道有 clojure.string/replace对于字符串:
user> (clojure.string/replace "fat cat caught a rat" "a" "AA")
;; => "fAAt cAAt cAAught AA rAAt"

向量和列表有类似的东西吗?

最佳答案

这对你有用吗?

(defn good-fnc [s sub r]
(loop [acc []
s s]
(cond
(empty? s) (seq acc)
(= (take (count sub) s) sub) (recur (apply conj acc r)
(drop (count sub) s))
:else (recur (conj acc (first s)) (rest s)))))

关于list - 替换子序列的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25690539/

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