aif.ss
#lang scheme/base

(require (for-syntax scheme/base))

(provide aif)

;;!
;; (aif name condition true-arm false-arm)
;; (aif name predicate condition true-arm false-arm)
;;
;; An anaphoric if (based on a pattern from On Lisp).  Like a normal
;; if, except the value of condition is bound to name in the scope of
;; the if
;;
;; E.g.
;;
;; (aif tail (member 'b '(a b c))
;;   tail
;;   'not-found)
;;
;; expands to
;;
;; (let ((tail (member 'b '(a b c))))
;;   (if tail
;;       tail
;;       'not-found))
;;
;; => '(a b c)
(define-syntax aif
  (syntax-rules ()
    ((_ name test true-arm false-arm)
     (let ((name test))
       (if name
           true-arm
           false-arm)))
    ((_ name pred test true-arm false-arm)
     (let ((name test))
       (if (pred name)
           true-arm
           false-arm)))
    ))