Ahoy,

On 25 August 2010 16:06, Glen Rubin <[email protected]> wrote:
> After toying around at the REPL I realize that I have been working
> with a heretofore invalid understanding of collections.  For example,
> working with the following collection(s):
>
> signal:
> (((1 2 3 4) (2 3 4 5) (3 4 5 6)) ((3 4 5 6) (4 5 6 7) (5 6 7 8)))
>
> I wanted to sum each individual list: e.g. (1 2 3 4) = (10)
>
> I thought I could do this as follows:
>
> (map #(map reduce + %) signal)
>
> This resulted in an error, so trying to comprehend why I ran the
> following:
>
> (map #(map identity (take 1 %)) signal)
>
> which results in,
> (((1 2 3 4)) ((3 4 5 6)))
>
> So, clojure sees 'signal' as 2 collections, whereas I thought it was a
> single collection.  This makes me concerned that I have been doing
> everything wrong thus far and getting computational errors. :(  So,
> how should I sum each individual list in the above collections?

It is a single collection, with two collections inside it:

user> (let [signal '(((1 2 3 4) (2 3 4 5) (3 4 5 6)) ((3 4 5 6) (4 5 6
7) (5 6 7 8)))]
           (do
            (println (nth signal 0))
            (println (nth signal 1))))
((1 2 3 4) (2 3 4 5) (3 4 5 6))
((3 4 5 6) (4 5 6 7) (5 6 7 8))

So don't you want to do this?

user> (map (fn [s] (map (fn [lst] (list (reduce + lst))) s)) signal)
(((10) (14) (18)) ((18) (22) (26)))

I'm not sure if that's the final data structure you're after - just
assuming from your e.g. (1 2 3 4) = (10) above.


Cheers,
David

-- 
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

Reply via email to