如何在Clojure中交错两个长度不同的向量 [英] how can you interleave two vectors of differing lengths in clojure
本文介绍了如何在Clojure中交错两个长度不同的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将两个向量与 n + 1
和 n
个成员进行交织的最简单方法是什么?
What is the simplest way to interleave two vectors with n+1
and n
members?
(def a [:a :c :e])
(def b [:b :d])
(interleave a b ); truncates to shortest list
[:a :b :c :d]
;what I would like.
(interleave-until-nil a b)
[:a :b :c :d :e]
推荐答案
第一个是利弊的,其余的则以相反的参数交织。
Cons the first, interleave the rest with arguments reversed.
(cons (first a) (interleave b (rest a)))
;=> (:a :b :c :d :e)
这篇关于如何在Clojure中交错两个长度不同的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文