On Tue, Dec 10, 2013 at 11:46 PM, Cedric Greevey <
cgre...@gmail.com> wrote:
> On Tue, Dec 10, 2013 at 6:29 PM, Vincent Chen <
nood...@gmail.com> wrote:
>>
>> Try this (not tested, might be missing parens and whatnot):
>>
>> (defn slice [x s n]
>> (loop [[h & tail] x, s s, n n, acc []]
>> (cond
>> (zero? n) acc
>> (zero? s) (recur tail s (dec n) (conj acc h))
>> :else (recur tail (dec s) n acc))))
>>
>> Few notes:
>> - When trying for tail recursions, use an accumulator. The accumulator
>> carries the eventual result, which is returned at the base case.
>> - Since we're destructuring into head and tail, I used vectors. conj
>> will push to the end of the vector.
>> - In Clojure, vectors tend to be more natural than lists. Accumulating
>> into lists usually requires a reverse in the base case.
>
>
> If you're going to go with vectors, why not go transient as well?
>
Ah. I've read about them, but have never used them before myself. Thanks.
By the way, my reply wasn't directed at you. I didn't know that you