syntax: (doif (sym expr) expr-yes expr-no))
expr is evaluated and assigned to sym
If then sym is not nil or (), then expr-yes is evaluated while sym is defined and can be used.
Otherwise expr-no is evaluated sym is still defined and can be used to distinguish between nil or ().
Code: Select all
(context 'doif)
(define-macro (doif:doif test do-yes do-no)
(eval
(list let test (list 'if (first test) do-yes do-no))))
(context MAIN)
Code: Select all
(let (l (function-can-return-nil))
(if l do-something-if-found
do-something-if-not))
Real world example:
Find a tag or an attribute in xml s-expression
Code: Select all
(define (xml-data tag xml)
(let (r (assoc 0 (map reverse (ref-all tag xml))))
(if r (xml (chop (reverse r))))))
Code: Select all
(doif (l (function-can-return-nil))
do-something-if-found
do-something-if-not))
Code: Select all
(define (xml-data tag xml)
(doif (r (assoc 0 (map reverse (ref-all tag xml))))
(xml (chop (reverse r)))))