Note that there are some explanatory texts on larger screens.

plurals
  1. POClojure refs odd behaviour
    text
    copied!<p>I feel like I'm missing something about the use of refs, something under the hood that isn't happening like I think it is. I'm making a thread-safe double linkedlist where the list is a hash containing refs to head and tail. A node of the list is represented by a hash containing previous, next and value. </p> <pre><code>(defn insert [list elem] (dosync (let [head @(:head list)] (if (nil? head) (do (ref-set (:head list) elem) (ref-set (:tail list) elem)) (do (ref-set (:head list) elem) (ref-set (:next elem) head);;fails! (ref-set (:prev head) elem))))));;fails! </code></pre> <p>This gives me a StackOverflow error. To be exact:</p> <p>StackOverflowError java.util.regex.Pattern$Curly.match </p> <p>It feels like I miss something in how refs work in Clojure, can anybody tell me what's wrong?</p> <hr> <p><strong>EDIT:</strong> Here is a link to the full "code". <a href="https://www.refheap.com/6544630b38b33b9d25d999829" rel="nofollow">https://www.refheap.com/6544630b38b33b9d25d999829</a> it fails on the last line, in the else clause of the insert function, when the head is not null anymore.</p> <pre><code>(def f (create-doubly-linked-list)) (def n (create-node 2)) (insert f n) (def m (create-node 3)) (insert (f m);;error! </code></pre> <p>Note that the error manifests when I try to <strong>access</strong> the list, or ie when I evaluate <strong>f</strong> in the repl, or when I execute the last line in the REPL.</p> <hr> <p><strong>UPDATE:</strong> Following code works, but accessing the list causes problems as well as printing it.</p> <pre><code>(def f (create-doubly-linked-list)) (def n (create-node 2)) (insert f n) (insert f (create-node 5)) (insert f (create-node 1)) (def m (create-node 3)) (insert f m) (is (= @(:value @(:tail f)) 2)) (is (= @(:value @(:head f)) 3)) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload