此代码:
(vector (first x)
(for [i (range (dec (count x)))] (+ (nth x i) (nth x (inc i))))
(last x)
)
对于x = [1 1],将返回[1(2)1]。如何在最佳实践中[1 2 1]而不是[1(2)1]?
答案 0 :(得分:2)
一种更简单的方法来做你想做的事:
(concat [(first x)] (map + x (rest x)) [(last x)])
或者如果它必须是矢量:
(into [(first x)] (conj (mapv + x (rest x)) (last x)))
答案 1 :(得分:-1)
作为一个方便的简写,相当于迭戈的答案,我通常会在这里使用语法引用的表达式:
`[~(first x)
~@(map + x (rest x))
~(last x)]
答案 2 :(得分:-2)
更加灵活的解决方案是:
(flatten [ (first x)
(map + x (rest x))
(last x) ] )