Location via proxy:   
[Report a bug]   [Manage cookies]                

anaphoric-if


The special type of syntax shown on syntax-aif is described in On-Lisp by Paul Graham. In the original version for Common-Lisp, it introduced a variable named it unhygienically. This was to resemble to the use of the natural language. For example, 'if I can get a package from the table, put it in the fridge.'

The version detailed on syntax-aif introduces hygiene by having the user specify the variable name explicitly; this loses some brevity but is cleaner. Also see alice-it.

Scheme, contrary to Common Lisp, supports a slight more verbose syntax for the same effect using the => syntax of cond clauses.

For example,

 (aif it (get-from table) 
      (put it fridge) 
      (sob)) 

translates to this R5RS Scheme (and note that aif is not in R5RS, any SRFI, or anything that might be considered related to Scheme standards set by the, such as SLIB):

 (cond ((get-from table) 
        => (lambda (it) 
             (put it fridge))) 
       (else (sob))) 

The second extension in this implementation over the original one is the introduction of a test procedure, which fits another quite common pattern:

 (aif it fresh? (get-from table) 
      (put it fridge) 
      (sob)) 

This is somewhat less easily translated to R5RS cond, and usually lacks the readability of this expression. The simplest R5RS translation is the code generated by this syntax translation:

 (let ((it (get-from table))) 
   (if (fresh? it) 
       (put it fridge) 
       (sob))) 

SRFI 61 generalizes the cond syntax, so this can be written as:

 (cond ((get-from table) fresh? 
        => (lambda (it) 
              (put it fridge))) 
       (else (sob))) 

Related syntax includes SRFI 2's and-let*, something of a combination between cond's => clauses and let*: it essentially makes a nesting of cond clause bindings linear.


category-code