| Here's the definition of aif from arc: (mac aif(expr . body)
`(let it ,expr
(if it
,@(if (cddr body)
`(,car.body (aif ,@cdr.body))
body))))
In all my recent playing around with macros I found those car/cdr manipulations jarring. Surely we can tease those variables apart? (mac aif(expr then . else)
`(let it ,expr
(if it
,@(if (cdr else) ; more conditions
`(,then (aif ,@else))
`(,then ,@else)))))
Much nicer. But in wart 'required' args are optional, so I can tease variables apart further and get rid of even that final cdr: (mac aif(expr then else . more-branches)
`(let it ,expr
(if it
,@(if more-branches
`(,then (aif `(else ,@more-branches)))
`(,then ,else)))))
Very cool.But it kinda sucks that I'm struggling to name that else variable. It's taking on different meanings in the last two lines of both the previous versions. Hmm, can I merge those two conditions somehow, get rid of the nested if? I could do that if aif did the right thing with just one arg. But that's weird - (aif 34) to return 34?! Hey, that's what arc does!! arc> (if 34)
34
arc> (aif 34)
34
wart> (if 34)
34
wart> (aif 34)
nil
I see, if expands to cond. wart does this as well for if, but I've now broken aif. Maybe it's not so weird after all. It's kinda convenient that the then of aif is it by default. Ok, let's maintain those semantics. (mac aif(expr then . more-branches)
(if then
`(let it ,expr
(if it
,then
(aif ,@more-branches)))
expr))
I can't think of a simpler spec for aif. "programs must be written for people to read, and only incidentally for machines to execute. So if a language is any good, source code ought to be a better way to convey ideas than English prose." http://arclanguage.org/item?id=13392(Narrative of commits 256-258 at http://github.com/akkartik/wart.) Coda: shouldn't iflet have the same behavior? arc> (iflet a 34)
Error: "procedure iflet: expects at least 3 arguments, given 2: 'a 34"
Ah, that's why aif was going the car/cdr route. In arc iflet needs an aif-like car/cdr based implementation. In wart it's a simple bugfix without giving up good variable names. |