On 11.01.2009, at 08:56, Tzach wrote:

> Following your good advice, I also update the next-cell function to
> work in a lazy way instead of sorting the values of the entire board.

Good idea!

> (defn next-cell [board]
>   "return the next potential cell to set, and the valid alternatives"
>   (first (for [n (range 1 10)]
>       (filter
>        #(= n (count (second %)))
>        (map-board board valid-values)))))

Your implementation has the disadvantage of recomputing (map- 
board ...) nine times. You can avoid this with a simple modification:

(defn next-cell [board]
   "return the next potential cell to set, and the valid alternatives"
   (let [vv (map-board board valid-values)]
     (first (for [n (range 1 10)]
          (filter
           #(= n (count (second %)))
           vv)))))

Since the values of the lazy sequence vv are cached, nothing will  
ever be recomputed.

However, I think there is also a bug in your function: (first  
(for ..)) will return the result of the first iteration of the for,  
even if it is emtpy. What you want is the first element of the first  
non-empty element of the for sequence. You can get this with (first  
(apply concat (for [n ...))). Concat again creates a lazy sequence,  
so nothing is computed unless necessary.

Konrad.


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