Clojure and ClojureScript REPL produce different output

Using the following recursive depth definition, first search for a Clojure (JVM) and ClojureScript (tested using both browsers connected to both lumo). REPL produces two different outputs, that is, the order in which nodes are printed is different and Clojure REPL creates a duplicate :f. The order of ClojureScript is the behavior I would expect. Why is this?

the code:

(defn dfs
  ([g v] (dfs g v #{}))
  ([g v seen]
   (println v)
   (let [seen (conj seen v)]
     (for [n (v g)]
       (if-not (contains? seen n)
         (dfs g n seen))))))

(def graph {:a [:b :c :e]
            :b [:d :f]
            :c [:g]})

(dfs graph :a)

CLRERE REPL output:

:a
:b
:c
:e
:d
:f
:g
:f
;; => ((() ()) (()) (()))

CLojureScript REPL output:

:a
:b
:d
:f
:c
:g
:e
;; => ((() ()) (()) ())
+4
source share
1 answer

Clojure for . dfs REPL, , .. ((() ()) (()) ()). (do (dfs graph :a) nil), :a.

Clojure 32 . , REPL ( str) for ( :b), seq , :c :e , ( ).

, Clojurescript (LazySeq IChunkedSeq) , , , .

- (first (for [i (range 300)] (do (println "printing:" i) i))) REPL Clojure, CLJS - 32 , Clojure, CLJS.

, doseq for for doall.

, .

: , @Josh, :f :f Clojure 1.8, parens cljs - ...

, , DFS. , i. . , doseq, , :

(defn dfs-eager
  ([g v] (dfs-eager g v #{}))
  ([g v seen]
   (println v)
   (let [seen (conj seen v)]
     (doseq [n (v g)]
       (if-not (contains? seen n)
         (dfs-eager g n seen))))))

, , . , for, , :

(defn dfs-lazy
  ([g v] (dfs-lazy g v #{}))
  ([g v seen]
   (cons v
         (let [seen (conj seen v)]
           (for [n (v g)]
             (if-not (contains? seen n)
               (dfs-lazy g n seen)))))))

(:a (:b (:d) (:f)) (:c (:g)) (:e)) - , . .

+6

Source: https://habr.com/ru/post/1670915/


All Articles