Hello,

I tried to define the sequence of primes via corecursion,
with trial divisions by primes p with p*p<=n,
finally arriving at following code.

(def primes
  (filter
    (fn isprime[n]
      (every?
        #(pos? (mod n %))
        (take-while #(<=(*%%)n) primes)
        )
      )
    (iterate inc 2)
    )
  )

Seems there's nothing wrong with that,
it's just the algorithm straightly set down
in a functional way. And it works correctly
as one can quickly confirm by "(take 50 primes)".

But if replacing "(iterate inc 2)" with "(drop 2 (range))",
we get strange behaviour, "primes" then consists of all!!
numbers from 2 until 30 followed by primes only from 31 on.

Being relatively new to clojure, I find this
very irritating and a potential cause of bad bugs.
And hope that this is not a bug in clojure itself
or even bad language design, but rather          
some basic misunderstanding by me.

So I analyzed it a bit, and it seems, that 
"range" triggers? execution of "drop" and "filter"
in batches of 32 numbers, and "primes" in "take-while"  
is not updated during such a batch. While using 
"iterate" instead updates "primes" each step.

Can someone more into clojure than me please correct and
better explain internal reasons of this strange behaviour.
How could one know the "batch size" of more complicated
expressions? And how could "range" be wrapped to yield
numbers one by one?

Thanks, Ulrich.


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

Reply via email to