Op woensdag 30 april 2014 12:14:39 UTC+2 schreef James Reeves: > > > On 30 April 2014 10:41, Roelof Wobben <rwo...@hotmail.com <javascript:>>wrote: > >> >> Op woensdag 30 april 2014 09:58:26 UTC+2 schreef James Reeves: >> >>> >>> Unlike vectors, seqs are simple structures and don't know their own >>> length. >>> >>> You can count seqs, but this involves iterating through every element in >>> turn. If all you want to do is find out whether there is more than one >>> element, there's a much simpler and more efficient way. >>> >>> >> Yep, if there is only 1 item is a list (rest coll) is nil. >> > > Almost. Clojure distinguishes between nil and the empty list. The function > you want is "next": > > (rest [1]) => () > (next [1]) => nil > > Why the distinction between next and rest? It has to do with laziness, and > if you're interested I can go into more detail. Otherwise just remember > that rest returns an empty list, and next returns nil. > > > (defn last* [coll] > (loop [coll coll] > (if (nil? (next coll)) > (first coll) > (recur (next coll)))) > > Consider how you might add a counter to the loop. You'll want to increment > the counter, then stop when it reaches the desired number. >
So without checking it so out of my head I would do this (defn last* [coll, number] (loop [coll coll] (if == counter number)) (first coll) (recur (next coll) (+counter 1 )) -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.