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

Taoufik Dachraoui dachraoui.taoufik at gmail.com
Thu Jul 26 12:08:28 PDT 2012


Hi

Thank you for the response

I did not like the compile so I came up with the following:

? (let ((xtype nil))
(defun mytype (e)
  (or (null e)
      (and (consp e)
           (typep (car e) `,xtype)
           (mytype (cdr e)))))
(deftype list-of (type)
  (setf xtype type)
  '(satisfies mytype)))
LIST-OF
? (typep '(2 3) '(list-of integer))
T
? (typep '(x y) '(list-of symbol))
T
? (typep '(x 3) '(list-of symbol))
NIL
?

Kind regards
Taoufik

On Thu, Jul 26, 2012 at 7:56 PM, Pascal J. Bourguignon <
pjb at informatimago.com> wrote:

> 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 {}.
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20120726/b1be862b/attachment.htm>


More information about the Openmcl-devel mailing list