Arc Forumnew | comments | leaders | submitlogin
aif bug and fix
18 points by byronsalty 5924 days ago | 6 comments
One would expect that if you use the multiple ifs in the else if then "it" should get remapped on the subsequent tests. For example:

  (aif 3 it 'else)
  3
So I'd think the following would be 3 also:

  (aif () 'a 3 it 'else)
  nil
In the original version of aif it would return "nil" as it never gets reset after the first test.

This is a simple fix:

Original:

  (mac aif (expr . body)
    `(let it ,expr (if it ,@body)))
Changed:

  (mac aif (expr . body)
    (if (<= (len body) 2)
      `(let it ,expr (if it ,@body))
      `(let it ,expr (if it ,(car body) (aif ,@(cdr body))))))
Should I commit this to the git repo?


4 points by nex3 5923 days ago | link

By all means commit this to the repo. The idea of a wiki is "push first, ask questions later." It's by no means critical that the wiki stay stable; that's what git://nex-3.com/arc.git is for.

-----

6 points by pg 5923 days ago | link

Ouch, that is a bug. Thanks.

-----

1 point by byronsalty 5916 days ago | link

So I had to see PG's solution in arc1. Pretty nice as it removes some duplication and has a simpler (and more efficient) way to determine if a sub aif is needed:

  (mac aif (expr . body)
     `(let it ,expr
       (if it
           ,@(if (cddr body)
                 `(,(car body) (aif ,@(cdr body)))
                 body))))
Duplication is removed by simple branching inside the `(let it ,expr ... part. And (cddr body) is shorter and more efficient than (<= (len body) 2).

-----

1 point by icemaze 5921 days ago | link

I believe it should be:

  (<= (len body) 1)
Also, this doesn't work with odd arguments. Working to fix that.

-----

1 point by icemaze 5921 days ago | link

Here it is:

  (mac aif args
    (let ln (len args)
      (if (is ln 0) nil
          (is ln 1) (car args)
          `(let it ,(car args)
              (if it ,(cadr args) (aif ,@(cddr args)))))))

-----

1 point by icemaze 5921 days ago | link

Er, ok, byronsalty's version works fine. Stupid me. :P

-----