[plt-scheme] Contract Request

From: Williams, M. Douglas (M.DOUGLAS.WILLIAMS at saic.com)
Date: Sun Apr 9 19:06:03 EDT 2006

I have begun using keywords in several of my collections.  One problem I
have is that there are no predefined contracts - that I know of, anyway -
for recognizing them.  Would it be possible to get 'keywords' added as a
contract that would be the same as 'symbols', but for matching against
keywords.

An implementation that works, based on the definition of symbols is:

  (define (keywords . ss)
    (unless ((length ss) . >= . 1)
      (error 'keywords "expected at least one argument"))
    (unless (andmap keyword? ss)
      (error 'keywords "expected keywords as arguments, given: ~a"
	     (apply string-append (map (lambda (x) (format "~e " x)) ss))))
    (flat-named-contract
     `(keywords ,@(map (lambda (x) x) ss))
     (lambda (x)
       (memq x ss))))

Doug



Posted on the users mailing list.