[Openmcl-devel] deftype list-of (recursive definition)

Pascal J. Bourguignon pjb at informatimago.com
Thu Jul 26 10:56:31 PDT 2012


Taoufik Dachraoui <dachraoui.taoufik at gmail.com> writes:

> Hi
>
> I would like to define a recursive type as follows:
>
> (deftype list-of (type)
>    `(or null (cons ,type (list-of ,type))))
>
> This does not work (stack overflow)
>
> How can define such types?


cl-user> (deftype list-of (type)
           (let ((funame (gensym (prin1-to-string `(list-of ,type)))))
             (compile funame `(lambda (x)  
                                (declare (ftype (function (t) t) ,funame))
                                (or (null x) 
                                    (and (consp x)
                                         (typep (car x) ',type)
                                         (,funame (cdr x))))))
             `(satisfies ,funame)))

list-of
cl-user> (typep '("one" "two" "three") '(list-of string))
t
cl-user> (typep '("one" 3 "three") '(list-of string))
nil
cl-user> 


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.




More information about the Openmcl-devel mailing list