Arc Forumnew | comments | leaders | submitlogin
1 point by d0m 5214 days ago | link | parent

Let's suppose however that I want to check if it is really in and not fruitloops.. Is there a way?

For example:

(foreach x in seq (prn x)) -> call (each x seq (prn x))

(foreach x seq (prn x)) -> call (each x seq (prn x))

(foreach x fruitloops seq (prn x)) -> (prn "mmmm")



1 point by evanrmurphy 5214 days ago | link

It's not pretty, but you could do:

  (mac foreach (var . args)
    (if (is car.args 'in)
          `(each ,var ,(cadr args)
             ,@(cddr args))
         (is car.args 'fruitloops)
          `(each ,var ,(cadr args)
             (prn "yum"))
         (acons car.args)
           `(each ,var ,(car args)
              ,@(cdr args))))
Update: I'm not sure it's any more clear, but another option would be,

  (mac foreach (var . args)
    `(each ,var ,(if (in car.args 'in 'fruitloops)
                      cadr.args
                     car.args)
       ,@(case car.args
           'in         cddr.args
           'fruitloops `((prn "yum"))
                       cdr.args)))
except that it's not working correctly for the 'fruitloops case and I can't figure out why.

Upperdate: Aha! It's because in and fruitloops in the case statement should not be quoted:

  (mac foreach (var . args)
    `(each ,var ,(if (in car.args 'in 'fruitloops)
                      cadr.args
                     car.args)
       ,@(case car.args
           in         cddr.args                       ; in not quoted
           fruitloops `((prn "yum"))                  ; ditto fruitloops
                      cdr.args)))
This works.

-----