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 :)
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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
> For more options, visit this group at
The 4clojure tests are intended to get you to write solutions without
the "simple, obvious" function. Sometimes they actually want you to
specifically write your own version of one of the core functions, to
show you understand how such functions really work.
When I took Amit Rathore's ProClojure Boot Camp course a year ago, the
first section was all about us writing our own versions of map,
reduce, filter and so on to make sure we understand the basics of
processing data structures.
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/
"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
user=> (source last)
:doc "Return the last item in coll, in linear time"
last (fn last [s]
(if (next s)
(recur (next s))
unless i'm missing something with multimethod or protocol magic :)
as long as (reversible? of-whatever-collection) is true , last is almost a constant time operation