On Sun, Jul 29, 2012 at 3:07 PM, John Holland <jbholl...@gmail.com> wrote:
> I'm doing some exercises in coding that are meant for Java but I'm doing
> them in Clojure. I'm stuck on this one. The goal is
> to return true if an array of ints contains two consecutive 2s. I figured
> I'd use Stuart Halloway's by-pairs function to turn the sequence
> into pairs of numbers and check for a pair that is 2,2.  This code in has22
> below works if pasted into the REPL and evaluated but as a
> function it always returns false. If anyone can explain my error to me it'd
> be great.
>
>
>
>
>
>
>
> (  defn by-pairs [coll] (let [take-pair (fn [c]
>                                          (when (next c) (take 2 c)))]
> (when-let [pair (seq (take-pair coll))]
>
> (lazy-seq
>
> (cons pair (by-pairs (rest coll)))))))
>
> (defn has22 [a]   (if (some true? (map  #(= 2 (first %) (nth % 1)) (by-pairs
> [a]))) true false))
>
>
>
> user> (some true? (map  #(= 2 (first %) (nth % 1)) (by-pairs [1 2 2 2 ])))
> true
>
>
> user> (has22 [1 2 2 2])
> false
>

In an effort to increase the net amount of perversity in the universe,
I offer the following alternate solution:

(defn has22 [a]
  (->> (concat [""] (map str a) [""])
       (interpose " ")
       (apply str)
       (re-find #" 2 2 ")
       boolean))

;-)

// Ben

-- 
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
  • Re: Mark Rathwell
    • Re: Moritz Ulrich
  • Re: Yoshinori Kohyama
    • Re: DeWitt Clinton
      • Re: nicolas.o...@gmail.com
        • Re: Yoshinori Kohyama
          • Re: nicolas.o...@gmail.com
            • Re: Yoshinori Kohyama
  • Re: Ben Smith-Mannschott

Reply via email to