Create the atom in a let, in the function - so it's new and isolated

better still - use an accumulator in the loop, not an atom.

take, map an others are lazy. you may need to (doall ...)

Dave


On Thursday, 10 January 2013 08:22:57 UTC+11, Simone Mosciatti wrote:
>
> Hi everybody,
>
> I was implementing a skip list, just for fun.
>
> I implemented the list and just to be sure that it were working I wanted 
> to count how many steps it compute to find a value.
> SkipList --> http://en.wikipedia.org/wiki/Skip_list
>
> To do so I was using a atom, and the code looks like this:
>
> (def c (atom 0))
>>
>  
>
>> (defn search-n-count [s n]
>>   (letfn [(search-fn [s n]
>>             (reset! c 0)  ;; I restart to count 
>>             (loop [s s]
>>               (swap! c inc)  ;; add a passage to the counter
>>               (when (seq s)  
>>                 (let [v (first s)]
>>                   (cond
>>                    (> v n) false
>>                    (== v n) n
>>                    (< v n) (recur (rest s)))))))]
>>     [(take 1 (drop-while #(or (false? %1) (nil? %1)) (map search-fn s 
>> (repeat n)))) @c]))  ;;the take and everything looks horrible, but I didn't 
>> find anything better, any ideas ?
>
>  
> However the value that I get simply doesn't makes any sense, sometimes it 
> looks like I get a value that could refer to the previous computation...
>
> Where is the problem ?
>
> If you don't get the code I can comment better the code, just let me 
> know...
>
> Here the whole code and some example: https://gist.github.com/4497077
>
> Thanks for any help :-)
>
> Simone
>

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