title: How to define type predicates in Scheme
body: <p>"Ordinary" functions are usually only defined on a domain of objects of a given type, but certain functions, such the Scheme type predicates <code>list?</code> or <code>procedure?</code>, are defined for arguments of any type, and can even be applied to themselves.  So e.g. <code>(list? procedure?)</code> evaluates to <code>#f</code> and <code>(procedure? procedure?)</code> evaluates to <code>#t</code>.  I am trying to figure out how one would write a totally defined function of this sort, but haven't been able to find a source that discusses this.</p>
  
  <p>For example, suppose we implement a pair data type as described in Exercise 2.4 of <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_sec_2.1.3" rel="nofollow">Structure and Interpretation of Computer Programs</a> using the following constructor and selectors:</p>
  
  <pre><code>    (define (cons x y)
        (lambda (m) (m x y)))
  
      (define (car z)
        (z (lambda (p q) p)))
  
      (define (cdr z)
        (z (lambda (p q) q)))
  </code></pre>
  
  <p>How would I then define a predicate <code>pair?</code> that returns <code>#t</code> for anything that was constructed using <code>cons</code>, and <code>#f</code> for anything that wasn't?  More generally, how are type predicates like <code>list?</code> and <code>procedure?</code> implemented?</p>