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
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
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
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
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
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 rubing...@gmail.com wrote:
I am working on
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
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
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 ]