On Fri, Jul 1, 2011 at 3:28 PM, .Bill Smith <william.m.sm...@gmail.com> wrote: > I want a concise function that, given an arbitrary length sequence, > determines whether the sequence is of consecutive integers starting with > one. So: > (f [1 2 3]) returns true > (f [1 2 4]) returns false > (f [0 1 2]) returns false > My first try, which I am not proud of, follows: > > (defn f [numbers] > (every? (fn [[x y]] (= x y)) (partition 2 (interleave (iterate inc 1) > numbers)))) > > Can someone suggest something better?
Fast: (defn iseq? [numbers] (loop [i 1 s (seq numbers)] (if s (if (== (first s) i) (recur (inc i) (next s))) true))) Possibly-unreadably clever: (defn iseq?2 [numbers] (and (= (count numbers) (count distinct numbers)) (every? identity (map #(get (into [] numbers) (dec %) nil) numbers)))) Simple: (defn iseq?3 [numbers] (= numbers (range 1 (inc (count numbers))))) All of these blow up on (iterate inc 1) of course and the first on any infinite seq. The other two may stop at the first non-matching value though. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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