syntax-aif


For a discussion about this syntax, see anaphoric-if.

Definition

 (define-syntax aif 
   (syntax-rules () 
     ;; simple version 
     ((aif var expr iftrue iffalse) 
      (let ((var expr)) 
        (if var 
            iftrue 
            iffalse))) 
     ;; version with a test procedure 
     ((aif var test expr iftrue iffalse) 
      (let ((var expr)) 
        (if (test var) 
            iftrue 
            iffalse))))) 

Usage:

repl> (define alist '((a 1) (b 2) (c 3)))
alist
repl> (aif R (assoc 'b alist) (cadr R) R)
2
repl> (aif R list? (assoc 'b alist) (reverse R) #f)
(2 b)

Description

Var is bound to the value of expr in iftrue if the value of expr satisfies the conditional; otherwise iffalse is evaluated.

The first form,

 (aif var expr 
      iftrue 
      iffalse) 

is equivalent to

 (cond (expr => (lambda (var) iftrue)) 
       (else iffalse)) 

The second form, (AIF var test expr iftrue iffalse), additionally tests the value of expr with test.


category-code