I'm fairly new too, but I'll take a stab.  I think the koan's for
19-21 are trying to make you do exactly what you are doing:  consider
the performance characteristics for various operations on a vector vs
a list or sequence.

Problem 19, for example, is highlighting that
1 - Last is slow for a vector.  The last function is really meant to
operate on sequences, and therefore is restricted to only being able
to use first/next in its implementation, so for a vector, it has to
walk the entire vector to get to the end.
2 - Vectors can efficiently get the last value with "peek".
So, for 19, it seems to make sense to convert to a vector and just peek:
#(peek (vec %))

For 21, the same is true:  nth is a sequence operation and therefore
must walk the vector.  The solution here is actually what you tried
(use get), but you'd need to convert the lists to a vector first:
#(get (vec %) %2)

So, my "lesson learned" here is:  If you need random access or, mostly
need access to the end of a fixed-length (ie non-infinite), use, or
convert to a vector.  But, again I'm new too :)

On Thu, Jun 9, 2011 at 8:50 PM, clojurefanxx <neuzhou...@gmail.com> wrote:
> 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

-- 
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

Reply via email to