gensym: hide the counter in an environment, define inc in stepA.
[jackhill/mal.git] / vb / stepA_mal.vb
index 39a2a1e..80d62c4 100644 (file)
@@ -278,8 +278,8 @@ Namespace Mal
             REP("(def! not (fn* (a) (if a false true)))")
             REP("(def! load-file (fn* (f) (eval (read-string (str ""(do "" (slurp f) "")"")))))")
             REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ""odd number of forms to cond"")) (cons 'cond (rest (rest xs)))))))")
-            REP("(def! *gensym-counter* (atom 0))")
-            REP("(def! gensym (fn* [] (symbol (str ""G__"" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))")
+            REP("(def! inc (fn* [x] (+ x 1)))")
+            REP("(def! gensym (let* [counter (atom 0)] (fn* [] (symbol (str ""G__"" (swap! counter inc))))))")
             REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))")
 
             If args.Length > fileIdx Then