i'm a newbie working thru 4clojure.com's problems #19 thru #21 where i'm asked to write a function, which when given a list or vector, returns the last, penultimate, or an arbitrary nth element, respectively.
for problem #19 (return last element), using the function last was not accepted as a good solution for both lists and vectors, but the following was accepted: user=> (#(nth % (- (count %) 1)) '(5 4 3)) 3 user=> (#(nth % (- (count %) 1)) '[1 2 3 4 5]) 5 for problem #21, (return arbitrary element) , using the function nth or get was not accepted. --------------------------------------------------------------------------------------------- Write a function which returns the Nth element from a sequence. (= (__ '(4 5 6 7) 2) 6) (= (__ [:a :b :c] 0) :a) (= (__ [1 2 3 4] 1) 2) (= (__ '([1 2] [3 4] [5 6]) 2) [5 6]) ------------------------------------------------------------------------------------------------ i think using the nth or get functions on vectors to return an element at index position n should be fine performance-wise, but what's a general solution for lists that will perform better than O(n)? (i 'm assuming that's the reason why last, nth, and get were rejected when I tried them out as candidate solutions??) thanks for any help! -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en