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
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
The other way around, actually -- the second and third will hang (in
count) on infinite seqs, the first stopping on any non-matching
item. Sorry, in a hurry here.
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to
On Fri, Jul 1, 2011 at 3:28 PM, .Bill Smith william.m.sm...@gmail.comwrote:
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
Here is another way:
(defn f [xs]
(and
(= 1 (first xs))
(apply = (map - (rest xs) xs
From: clojure@googlegroups.com [mailto:clojure@googlegroups.com] On
Behalf Of David Nolen
Sent: Friday, July 01, 2011 3:51 PM
To:
On Fri, Jul 1, 2011 at 3:51 PM, David Nolen dnolen.li...@gmail.com wrote:
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
On Fri, Jul 1, 2011 at 4:04 PM, Chouser chou...@gmail.com wrote:
(defn f [xs] (every? true? (map = xs (iterate inc 1
--Chouser
Hrm, shoulda thought 'o that :)
David
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send
I see some more clever ways have been posted.
Perhaps a more interesting, general problem: detect if the input is an
arithmetic sequence at all.
Here's a few implementations to get you started.
Straightforward:
(let [diffs (map - xs (rest xs))]
(every? identity (map = diffs (rest diffs
On Fri, Jul 1, 2011 at 4:04 PM, Chouser chou...@gmail.com wrote:
(defn f [xs] (every? true? (map = xs (iterate inc 1
--Chouser
Also,
(defn f [xs] (every? #{1} (map - xs (iterate inc 0
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To
Thanks everyone.
--
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,
On Jul 1, 1:41 pm, David Nolen dnolen.li...@gmail.com wrote:
On Fri, Jul 1, 2011 at 4:04 PM, Chouser chou...@gmail.com wrote:
(defn f [xs] (every? true? (map = xs (iterate inc 1
--Chouser
Also,
(defn f [xs] (every? #{1} (map - xs (iterate inc 0
This has problems before 1.3 if
Using map-indexed:
(defn f [xs] (every? true? (map-indexed #(= (inc %1) %2) xs)))
On Jul 1, 12: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.
12 matches
Mail list logo