Andrew Rafas <andras.ra...@gmail.com> writes:

Hi Andrew,

> Just created this solution for the Number Maze problem on 4clojure:
> http://www.4clojure.com/problem/106
>
> (
>   (fn [a b]
>     (letfn [(nummaze [a b len]
>                      (cond
>                        (= len 0) false
>                        (= a b) true
>                        (nummaze (* 2 a) b (dec len)) true
>                        (and (even? a) (nummaze (quot a 2) b (dec len))) true
>                        (nummaze (+ 2 a) b (dec len)) true
>                        :else false))]
>     (first (filter #(nummaze a b %) (range 15)))))
>     ;(first (filter #(nummaze a b %) (range)))))
> 9 2)
>
> Normally the program returns instantly. The strange thing is that if I
> uncomment the line with the unrestricted (range) then it churns for
> minutes but gives the correct answer. So it is evaluated lazily since
> it gives an answer.
>
> Is it a codegen bug or just the effect of chunked sequences? Is so why?

Seems to be the result of chunked seqs where the first 32 items are
realized although you only take one.  For i < 20 (nummaze 9 2 i) is fast
but it gets costly quite soon.

Fogus once posted some de-chunkifying function `seq1` for such use-cases
at

  http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/

Putting his `seq1` around the `(range)` solves the issue here.

BTW, in his blog-post he said that one day, there will be an official
de-chunkifying in Clojure, but AFAICS there's none right now, right?

I think, there are several use-cases like the one above where you know
exactly that realization is very costly and should be avoided if not
needed.

Bye,
Tassilo

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