gcl-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gcl-devel] list types


From: Pascal Bourguignon
Subject: Re: [Gcl-devel] list types
Date: Tue, 28 Jun 2005 15:59:27 +0200

Camm Maguire writes:
> Greetings!  Is there a type specifier which restricts elements of a
> list, e.g. '(list symbol)?

Not in Common Lisp.

But in Common Lisp, you can write such a type specifier:

[41]> (typep '(1 2 3) '(restricted-list fixnum))
T
[42]> (typep '(1 2 3) '(restricted-list (integer 1 2)))
NIL
[43]> (typep '(1 2 3) '(restricted-list (integer 1 5)))
T

The only difficulty is that SATISFIES expects a symbol and nothing
else.  So we will have to create such symbols on the fly.  May be it
could be possible to only use GENSYM's or uninterned symbols, but I
don't think the specification says it should work always, so to be on
the safe side, we'll intern the predicate symbols in a special
package:


(defpackage "$$RESTRICTED-LIST-PREDICATES$$" (:USE))


(defun find-restricted-list-predicate (element-type)
  (let* ((name (with-standard-io-syntax (format nil "~S-P" element-type)))
         (predicate (FIND-SYMBOL name "$$RESTRICTED-LIST-PREDICATES$$")))
    (unless predicate
      (setf predicate (intern name "$$RESTRICTED-LIST-PREDICATES$$"))
      (eval `(defun ,predicate (list)
               (every (lambda (item) (typep item ',element-type)) list))))
    predicate))


(deftype restricted-list (element-type)
  `(or null
       (and cons
            (satisfies ,(find-restricted-list-predicate element-type)))))


             

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

The world will now reboot.  don't bother saving your artefacts.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]