将元素添加到向量(conj函数)并保留更改

时间:2018-12-28 11:04:45

标签: clojure

我的函数的目标是读取输入的解释向量并在分析后返回一个值。

要实现我的目标,我需要将字符串添加到向量中并保留更改。 conj函数会返回一个新向量,但实际上不会更改我的变量 eq_code

(conj eq_code(获取矢量[3 1]))不会更改矢量 eq_code 的值。我认为对变量 eq_code 重复执行let函数不会解决我的问题。

这是我的一部分代码:

(defn interpreted-lang-while [interpreting-vector]
  (let [vector interpreting-vector
    ;before_if code added to eq_code
    eq_code [(str (get-in vector [1 1]) "0;")]]


    (def interpret-eval-eq_code (->> (apply str eq_code) lang1-parser lang1-interpret)) 


    (while 
      ;eval the code (last value contains condition)
      (not= 0 (do (conj eq_code (str (get-in vector [2 1]) ";"))(interpret-eval-eq_code) (prn (str "Eval condition : " (apply str eq_code)))))
      ;if the last value is not 0 =>add the statement_OK and eval the condition again
      (do (conj eq_code (get-in vector [3 1])) (prn (str "End loop code : " (apply str eq_code)))))
     (conj vector (str (interpret-eval-eq_code) ";")) 

;last values contains 0 =>end of the while
(prn (str "Code equivalent : " eq_code)) 
(conj eq_code (get-in vector [4 1])) 
(interpret-eval-eq_code)))

我使用clojure执行以下行:

 (interpreted-lang-while [:LangWHILE [:before_while "a=1; b= a+2; c="] [:condition_expression "a-3"] [:statements_OK "a=a+1;b=b+2;"] [:after_while " c+1;"]])

我在终端上得到以下输出:

project.core=> "Eval condition : a=1; b= a+2; c=0;"
"End loop code : a=1; b= a+2; c=0;"
"Eval condition : a=1; b= a+2; c=0;"
"End loop code : a=1; b= a+2; c=0;"
"Eval condition : a=1; b= a+2; c=0;"
"End loop code : a=1; b= a+2; c=0;"
<Infinite loop ...>

代替

project.core=> "Eval condition : a=1; b= a+2; c=0;a-3;"
"End loop code : a=1; b= a+2; c=0;a-3;a=a+1;b=b+2;"
"Eval condition : a=1; b= a+2; c=0;a-3;a=a+1;b=b+2;a-3;"
<And so on ..>

您能建议我一种向向量添加新元素并更改变量 eq_code 的方法吗? 感谢您的帮助。

0 个答案:

没有答案