Thursday, February 13, 2014

99 Clojure Problems – 21: Insert an Element at a given Position into a List

Example:

(deftest p21-insert-at
  (is (= '(1 new 2 3 4) (insert-at 'new 1 '(1 2 3 4)))))

Solution:

My solution to this problem is based on the same idea as the previous one: Reuse split from problem 17 to cut the input sequence into two parts. Then concatenate the parts, cons'ing the element to insert to the tail before doing so.

Read more about this series.

No comments: