I am struggling to find a function that will find a happy-medium between the get function and nth. I have been doing a lot of research on these sequence type functions, does anyone know a work around for this or know a function that performs as such?
I need nth's ability to grab sublists:
=> (nth '(1 (2 3) 4 5) 1)
(2 3)
=> (get 1 '(1 (2 3) 4 5))
nil
And I need get's ability to return "nil" when out of range:
=> (get -1 '(1 (2 3) 4 5))
nil
=> (nth '(1 (2 3) 4 5) -1)
Execution error (IndexOutOfBoundsException) at user/eval149 (REPL:1).
null
I need this code for a recursive sub-seq function:
(defn sub-seq [coll i j]
(nth coll i)
(if (< i (+ i j))
(sub-seq coll (inc' i) j)
)
)
(The sub-seq function is supposed to return 'j' elements starting at position 'i'.)
Here are some sample outputs for what I am trying to write:
=> (sub-seq '(1 2 (3 4) (5 (6 7))) 1 2))
(2 (3 4))
=> (sub-seq '(1 2 3 4 5 6 7) 2 4)
(3 4 5 6)
I finally got my function to work, thank you all for your help:
(defn sub-seq [coll i j]
(conj
(list*
(nth coll i nil)
(if (> j 1)
(sub-seq coll (+ i 1) (- j 1))))))