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))))
Ultra-sneaky:
(= 1 (count (distinct (map - xs (rest xs)))))
Speedy Gonzales:
(let [r (next xs)
s (first r)
diff (- s (first xs))]
(loop [f s r (next r)]
(if xs
(let [s (first r)]
(if (== diff (- s f))
(recur s (next r))))
true)))
--
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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en