将元素添加到嵌套向量

时间:2017-10-19 22:29:42

标签: vector clojure io

我不知道如何完成这项工作,也许有人可以帮助我?我想做这样的事情:

vector<int> L[MAX_V];
for(int i=1;i<N;++i){
    scanf("%d %d",&u,&v);
    --u; --v;
    L[u].push_back(v);
    L[v].push_back(u);
}

,但用clojure语言。到目前为止,我已经得到了类似的东西,但它不起作用:

(defn LoadTree []
  (def n (read-string (read-line)))
  (def tree (atom (into [] (repeat n []))))
  (loop [x n]
    (when (> x 1)
      (let [input (read-string (str "[" (read-line) "]"))]
        (swap! (get @tree (dec (get input 0))) conj (dec (get input 1))) <-Error
        (swap! (get @tree (dec (get input 1))) conj (dec (get input 0))) <-Error
      (recur (dec x))
      )
    )
  )

1 个答案:

答案 0 :(得分:1)

我会写这样的东西:

(defn foo [num-reads size]
  (loop [i num-reads, r (vec (repeat size []))]
    (if (zero? i) 
      r
      (let [[u v] (repeatedly read)]
        (recur (dec i), (-> r (update u conj v) (update v conj u)))))))