Arc Forumnew | comments | leaders | submitlogin
3 points by Oscar-Belletti 2991 days ago | link | parent

Now the next function returns the next index and not the next element. iter still evaluates list two times and els can cause variable capture.

    arc> (mac iter (var list . body)
          (let i (uniq)
            `(if (is nil ,list)
               nil
               (withs (els ,list
                       ,i 0
                       ,var (els ,i)
                       next (fn () (++ ,i)))
                 (while (< ,i (len els))
                   (= ,var (els ,i))
                   ,@body
                   (next))))))
    #(tagged mac #<procedure: iter>)
    arc> (iter a '(1 2 3) (prn a))
    1
    2
    3
    nil
    arc> (iter els '(1 2 3) (prn els))
    Error: "car: contract violation\n  expected: pair?\n  given: 1"
    arc> (iter a '(a b c d) (prn a ", " (next)))
    a, 1
    c, 3
    nil
This should fix the problems:

    (mac iter (var lst . body)
        (w/uniq (i elst)
          `(let ,elst ,lst
            (if (is ,elst nil)
              nil
              (withs (,i 0
                      ,var (,elst ,i)
                      next (fn ()
                             (++ ,i)
                             (= ,var (,elst ,i))))
                (while (< ,i (len ,elst))
                  (= ,var (,elst ,i))
                  ,@body
                  (++ ,i)))))))


3 points by mpr 2990 days ago | link

Thanks again!

-----

2 points by Oscar-Belletti 2990 days ago | link

My pleasure

-----