Arc Forumnew | comments | leaders | submitlogin
acond for Arc
5 points by kostas 6152 days ago | 6 comments
I am reading parts of On Lisp and playing around with Arc and as a simple exercise I translated the acond in On Lisp to Arc. For people new to the idea of macros (like me), I think this is a good starting exercise. I suggest you try it yourself before looking at the Arc version.

The existing On Lisp code is:

   (defmacro acond (&rest clauses) 
     (if (null clauses) 
         nil 
         (let ((cl1 (car clauses)) 
               (sym (gensym))) 
           `(let ((,sym ,(car cl1))) 
              (if ,sym 
                  (let ((it ,sym)) ,@(cdr cl1)) 
                  (acond ,@(cdr clauses)))))))
My port to Arc is:

  (mac acond clauses
    (if (no clauses)
      nil
      (w/uniq g
        (let cl1 (car clauses)
          `(let ,g ,(car cl1)
             (if ,g
               (let it ,g ,@(cdr cl1))
               (acond ,@(cdr clauses))))))))
A quick test to verify that acond works is:

  (= airport (table))
  (= (airport 'yyz) "Toronto")
  (acond ((airport 'sfo) (prn "This won't print " it))
         ((airport 'yyz) (prn "I'm flying to " it)))


9 points by drcode 6152 days ago | link

  (mac acond clauses
      (when clauses
        (with (((a . b) . c) clauses
               g (uniq))
          `(iflet ,g ,a
               (let it ,g ,@b)
               (acond ,@c)))))

-----

1 point by kostas 6150 days ago | link

Very nice. Thanks.

-----

4 points by fallintothis 6151 days ago | link

Though, of course, the idea in Arc is to use aif and avoid all those unnecessary parentheses as in cond:

  (= airport (table))
  (= (airport 'yyz) "Toronto")
  (aif (airport 'sfo) (prn "This won't print " it)
       (airport 'yyz) (prn "I'm flying to " it))
On the other hand, it's good that you can easily extend Lisps with virtually any control structure you want. To each their own, so far as the parentheses-laden cond vs. parentheses-lacking if issue goes.

-----

1 point by kostas 6150 days ago | link

I was wondering why acond wasn't included in arc.arc. Now I see why. The Arc aif is definitely better. Thanks.

-----

3 points by vrk 6152 days ago | link

You could get rid of one nesting level by using with and uniq:

  (with (cl1 (car clauses)
         g (uniq))
     `(let ,g...
This will make the resemblance even stronger, since the Arc with is basically the same as CL let, sans extra parentheses.

-----

1 point by mecon 6151 days ago | link

Great Song

-----