Re: collections within collections

2010-03-11 Thread Christophe Grand
Hi all, Since I'm fp-phobic when doing arithmetic :-) here is mine: (defn pythagorean-triples [] (for [a (iterate inc 1) :let [squares (into {} (for [c (range a (* 1.5 a))] [(* c c) c]))] b (range 1 a) :let [c (squares (+ (* a a) (* b b)))] :when c] [a b c])) Ch

Re: collections within collections

2010-03-11 Thread Wilson MacGyver
if you use (map f coll), with only 1 collection. it takes every item in the collection, and apply f to it. so what I'm really doing is, taking "1 item" at a time from the collection. but "1 item" turns out to be a collection. I then apply a filter to it so it only returns what I want. On Thu, Mar

Re: collections within collections

2010-03-11 Thread Tyler Perkins
I'm afraid additional verbosity is required. But thanks to your excellent book (page 229), I know how to import sqrt and floor: (use '[clojure.contrib.import-static :only (import-static)]) (import-static java.lang.Math sqrt floor) ;;; ... your Clojure code ... -- You received this message becau

Re: collections within collections

2010-03-11 Thread Glen Rubin
it seems kind of weird that map would work like that. i understood map as taking the first item of every collection and applying f to it. e.g (map + [2 4] [6 8]) -> [8 12] In this case, since answer? takes a collection as argument I guess map just applies answer? to each collection? but don't

Re: collections within collections

2010-03-11 Thread Stuart Halloway
The same strategy works in Clojure, of course: (defn b-of-a [a] (/ (* 1000.0 (- 500.0 a)) (- 1000.0 a))) (defn near-int? [x] (< (- x (floor x)) 0.1)) (first (for [a (iterate inc 1) :let [b (b-of-a a)] :when (near-int? b)] [a b (sqrt (+ (* a a) (* b b)))])) Stu I like Clojure, but

Re: collections within collections

2010-03-10 Thread Tyler Perkins
I like Clojure, but as a point of comparison, here's a Haskell solution, as typed in the REPL: Prelude> let bOf a = 1000*(500 - a)/(1000 - a) Prelude> let nearInt x = x - fromInteger(truncate x) < 0.01 Prelude> head [ ( a, b, sqrt(a^2 + b^2) ) | a <- [1..], b <- [bOf a], nearInt b ] (200.0

Re: collections within collections

2010-03-10 Thread Michael Gardner
On Mar 10, 2010, at 12:20 PM, Glen Rubin wrote: > However, the output of my trips function yields multiple collections > of vectors inside of a larger vector. I am completely befuddled as to > how to process this behemoth. You can merge the structure into a single list of triples by applying con

Re: collections within collections

2010-03-10 Thread Stuart Halloway
Hi Glen, When your are working with infinite sets in Clojure, is it better to take advantage of laziness as far as possible, instead of passing limits such as the coll argument to trips. Here's how I would think about this problem: (defn pairs-adding-to-n "Pairs of distinct positive int

Re: collections within collections

2010-03-10 Thread Wilson MacGyver
you can define a function to filter the result like (defn answer? [x] (filter #(every? integer? %) x)) and then just call it by doing user=> (map #(answer? %) (trips (range 1 7))) (() () ([3 4 5]) () ()) On Wed, Mar 10, 2010 at 1:20 PM, Glen Rubin wrote: > I am working on the following proble

collections within collections

2010-03-10 Thread Glen Rubin
I am working on the following problem: Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000 My strategy is to produce a series of triplets of a^2 + b^2 and then filter out the ones where the c^2 is a perfect square, in order to determine Pythagorean triplets. I wrote a fun